codeforces 460B Little Dima and Equation 解题报告

题目链接: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 }

 

posted @ 2014-08-21 09:42  windysai  阅读(293)  评论(0编辑  收藏  举报