B. Little Dima and Equation

B. Little Dima and Equation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment.

Find all integer solutions x (0 < x < 109) of the equation:

x = b·s(x)a + c, 

where abc are some predetermined constant values and function s(x) determines the sum of all digits in the decimal representation of number x.

The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: abc. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem.

Input

The first line contains three space-separated integers: a, b, c (1 ≤ a ≤ 5; 1 ≤ b ≤ 10000;  - 10000 ≤ c ≤ 10000).

Output

Print integer n — the number of the solutions that you've found. Next print n integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 109.

Sample test(s)
input
3 2 8
output
3
10 2008 13726 
input
1 2 -18
output
0
input
2 2 -1
output
4
1 31 337 967 

题目链接:http://codeforces.com/problemset/problem/460/B

题目意思:给出a, b, c三个数,要你找出所有在 1 ≤ x ≤ 1e9 范围内满足 x = b·s(x)a +  这条等式的x的个数,并输出相应的 x 具体是多少。

    不看tutorial 都不知道,原来枚举的方向错了,人家是枚举1~81 的情况,我就是枚举1~1e9, = =。。。直接暴力即可,有个比较要注意的地方,算方程右边的时候有可能超过int,需要用long long 或 __int64 保存。

    (1)long long 版

    

复制代码
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 1e6;   // 不知道符合的x有多少个,尽量开大一点吧
 8 const int maxx = 1e9;
 9 const int minx = 1;
10 
11 typedef long long LL;
12 int ans[maxn];
13 LL tx;
14 
15 int main()
16 {
17     int a, b, c;
18     while (scanf("%d%d%d", &a, &b, &c) != EOF)
19     {
20         int cnt = 0;
21         for (int i = 1; i <= 81; i++)  // 枚举1~999999999每位数字和
22         {
23             int sx = i;
24             int p = i;
25             for (int j = 1; j < a; j++)
26                 sx *= p;
27             tx = (LL)sx*b + (LL)c;
28             if (tx > maxx || tx < minx)
29                 continue;
30             int x = sx*b + c;
31             int tot = 0;
32             while (x)
33             {
34                 tot += x%10;
35                 x /= 10;
36             }
37             if (tot == i)
38                  ans[cnt++] = sx*b + c;
39             }
40         }
41         printf("%d\n", cnt);
42         for (int i = 0; i < cnt; i++)
43             printf("%d ", ans[i]);
44     }
45     return 0;
46 }
复制代码

 

(2) __int64 版本

复制代码
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 1e6;
 8 const int maxx = 1e9;
 9 const int minx = 1;
10 
11 int ans[maxn];
12 __int64 tx, a, b, c;
13 
14 int main()
15 {
16     while (scanf("%I64d%I64d%I64d", &a, &b, &c) != EOF)
17     {
18         int cnt = 0;
19         for (int i = 1; i <= 81; i++)  // 枚举1~999999999每位数字和
20         {
21             __int64 sx = i;
22             __int64 p = i;
23             for (int j = 1; j < a; j++)
24                 sx *= p;
25             tx = sx*b + c;
26             if (tx > maxx || tx < minx)
27                 continue;
28             __int64 x = sx*b + c;
29             int tot = 0;
30             while (x)
31             {
32                 tot += x%10;
33                 x /= 10;
34             }
35             if (tot == i)
36                  ans[cnt++] = sx*b + c;
37         }
38         printf("%d\n", cnt);
39         for (int i = 0; i < cnt; i++)
40             printf("%d ", ans[i]);
41     }
42     return 0;
43 }
复制代码

 

    总结:long long 写起来好像比 __int64 简单一些啦

 

    这个是参考作者写的,本人更喜欢作者的写法,每个函数有各自的功能,而且比较清晰,很奇怪的是,用codeblocks 检验第 三 组 数据 2 2 1 的时候,我的电脑一直输出0,用custom test 可以得出正确结果。

   

复制代码
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <vector>
 5 using namespace std;
 6 
 7 typedef long long ll;
 8 vector<ll> ans;
 9 ll a, b, c;
10 
11 ll S(ll p, ll a)
12 {
13     ll s = 1;
14     for (int i = 1; i <= a; i++)
15         s *= p;
16     return s;
17 }
18 
19 ll Digit(ll x)
20 {
21     ll wei = 0;
22     while (x)
23     {
24         wei += x % 10;
25         x /= 10;
26     }
27     return wei;
28 }
29 
30 int main()
31 {
32     int len = 0;
33     while (scanf("%lld%lld%lld", &a, &b, &c) != EOF)
34     {
35         for (ll i = 0; i < len; i++)
36             ans.clear();
37         for (ll i = 1; i <= 81; i++)
38         {
39             ll x = b*S(i, a) + c;
40             if (x < 1e9 && Digit(x) == i)
41                 ans.push_back(x);
42         }
43         printf("%d\n", ans.size());
44         for (int i = 0; i < ans.size(); i++)
45             printf("%lld ", ans[i]);
46         len = ans.size();
47     }
48     return 0;
49 }
复制代码

 #include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,i,j,d[1000],tx,t,cnt,num,x; 
while(scanf("%d%d%d",&a,&b,&c)!=EOF)
{
cnt=0;
for(i=1;i<=81;i++)
{
tx=1;
for(j=1;j<=a;j++)
tx*=i;
x=b*tx+c;
if(x>=1e9)
break;
num=x;
t=0;
while(num!=0)
{
t+=num%10;
num/=10;
}
if(t==i)
{
cnt++;
d[cnt]=x;
}
}
printf("%d\n",cnt);
for(i=1;i<=cnt;i++)
printf("%d ",d[i]);
if(cnt)
printf("\n");
}
return 0;
}

标签:  implementation math

你可能感兴趣的:(优化)