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

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = 1e6; // 不知道符合的x有多少个,尽量开大一点吧
const int maxx = 1e9;
const int minx = ; typedef long long LL;
int ans[maxn];
LL tx; int main()
{
int a, b, c;
while (scanf("%d%d%d", &a, &b, &c) != EOF)
{
int cnt = ;
for (int i = ; i <= ; i++) // 枚举1~999999999每位数字和
{
int sx = i;
int p = i;
for (int j = ; j < a; j++)
sx *= p;
tx = (LL)sx*b + (LL)c;
if (tx > maxx || tx < minx)
continue;
int x = sx*b + c;
int tot = ;
while (x)
{
tot += x%;
x /= ;
}
if (tot == i)
ans[cnt++] = sx*b + c;
}
}
printf("%d\n", cnt);
for (int i = ; i < cnt; i++)
printf("%d ", ans[i]);
}
return ;
}

(2) __int64 版本

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = 1e6;
const int maxx = 1e9;
const int minx = ; int ans[maxn];
__int64 tx, a, b, c; int main()
{
while (scanf("%I64d%I64d%I64d", &a, &b, &c) != EOF)
{
int cnt = ;
for (int i = ; i <= ; i++) // 枚举1~999999999每位数字和
{
__int64 sx = i;
__int64 p = i;
for (int j = ; j < a; j++)
sx *= p;
tx = sx*b + c;
if (tx > maxx || tx < minx)
continue;
__int64 x = sx*b + c;
int tot = ;
while (x)
{
tot += x%;
x /= ;
}
if (tot == i)
ans[cnt++] = sx*b + c;
}
printf("%d\n", cnt);
for (int i = ; i < cnt; i++)
printf("%d ", ans[i]);
}
return ;
}

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

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

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std; typedef long long ll;
vector<ll> ans;
ll a, b, c; ll S(ll p, ll a)
{
ll s = ;
for (int i = ; i <= a; i++)
s *= p;
return s;
} ll Digit(ll x)
{
ll wei = ;
while (x)
{
wei += x % ;
x /= ;
}
return wei;
} int main()
{
int len = ;
while (scanf("%lld%lld%lld", &a, &b, &c) != EOF)
{
for (ll i = ; i < len; i++)
ans.clear();
for (ll i = ; i <= ; i++)
{
ll x = b*S(i, a) + c;
if (x < 1e9 && Digit(x) == i)
ans.push_back(x);
}
printf("%d\n", ans.size());
for (int i = ; i < ans.size(); i++)
printf("%lld ", ans[i]);
len = ans.size();
}
return ;
}

最新文章

  1. 如果你也会C#,那不妨了解下F#(2):数值运算和流程控制语法
  2. VS2015 Android
  3. leetcode 141
  4. Servlet里写验证码
  5. yii 验证码的使用
  6. 开始→运行(cmd)命令大全
  7. Asp.NET MVC 中使用 SignalR 实现推送功能
  8. Codeforces Gym 100803F There is No Alternative 暴力Kruskal
  9. SCALA编程实例
  10. Python Requests 库学习笔记
  11. Cocos2D iOS之旅:如何写一个敲地鼠游戏(十一):完善游戏逻辑
  12. hibernate坑边闲话3
  13. python3 + selenium 之元素定位
  14. Slapper帮助Dapper实现一对多
  15. Windows常用的CMD命令
  16. nginx: [error] open() &quot;/run/nginx.pid&quot; failed (2: No such file or directory)
  17. 解决:SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间提示问题
  18. Android Studio安装、配置教程全 - 安卓开发环境的配置手册
  19. 初识powershell、nuget powershell 调试
  20. XPO开发指南简要

热门文章

  1. AnyChart图表仪表控件在Flex环境下使用
  2. 很好的linux下GPIO驱动详解文章
  3. Poi写文件时报java.io.IOException: Read error
  4. Laravel 控制器的middleware中间件
  5. Day 11 正则表达式
  6. BZOJ——1610: [Usaco2008 Feb]Line连线游戏
  7. ImportError: No module named _curses;Color support is disabled, python-curses is not installed.解决办法
  8. [HTML5] Show Different Variations of Images Depending on the Viewport Width using Art Direction
  9. 一个能自己主动搜索源文件并自己主动推导的Makefile
  10. Android中View自己定义XML属性具体解释以及R.attr与R.styleable的差别