codeforces 460B Little Dima and Equation 解题报告
题目链接:http://codeforces.com/problemset/problem/460/B
题目意思:给出a, b, c三个数,要你找出所有在 1 ≤ x ≤ 1e9 范围内满足 x = b·s(x)a + c 这条等式的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 ;
}
codeforces 460B Little Dima and Equation 解题报告的更多相关文章
- 【LeetCode】640. Solve the Equation 解题报告(Python)
[LeetCode]640. Solve the Equation 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- codeforces C1. The Great Julya Calendar 解题报告
题目链接:http://codeforces.com/problemset/problem/331/C1 这是第一次参加codeforces比赛(ABBYY Cup 3.0 - Finals (onl ...
- codeforces B. Eugeny and Play List 解题报告
题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...
- codeforces 433C. Ryouko's Memory Note 解题报告
题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...
- codeforces 556B. Case of Fake Numbers 解题报告
题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...
- codeforces 510B. Fox And Two Dots 解题报告
题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
- codeforces 505A. Mr. Kitayuta's Gift 解题报告
题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母 ...
- codeforces 499A.Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/499/A 题目意思:有两种按钮:1.如果当前观看的时间是 t,player 可以自动处理下一分钟,姑且理解 ...
- ACM:HDU 2199 Can you solve this equation? 解题报告 -二分、三分
Can you solve this equation? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
随机推荐
- 洛谷P2365 任务安排 [解法一]
题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间是Ti.在每批任务开始 ...
- 51nod1053 最大M子段和 V2
$n \leq 50000$的序列,问选不超过$m \leq 50000$个区间使得和最大. 如果正数区间总数比$m$小那肯定全选.否则有两种方式减少区间数量:丢掉一个正区间:补一个负区间连接两个正区 ...
- 巧克力王国 BZOJ 2850
巧克力王国 [问题描述] 巧克力王国里的巧克力都是由牛奶和可可做成的.但是并不是每一块巧克力都受王国人民的欢迎,因为大家都不喜欢过于甜的巧克力.对于每一块巧克力,我们设x和y为其牛奶和可可的含量.由于 ...
- laravel 查询构造器
//查询构造器public function query(){ $bool = DB::table('student')->insert([ ['name' => '王五', 'age' ...
- laravel控制器
//访问MemberController下的info的方法 //方法一//访问路径http://localhost/Laravel/public/member/infoRoute::get('memb ...
- Windows 10安装IntelliJ IDEA时快捷键冲突设置
Windows的快捷键的非常多,而且个性化软件获得这些权限的也很多,所以没有最终的方法,只能不断的发现和尝试. 下面是收集的一些教程,或许能在这里找到灵感: 切记:不建议优先修改IDEA的快捷键,应该 ...
- 使用Myeclipse + SVN + TaoCode 免费实现项目版本控制的详细教程
通过Myeclipse + SVN插件 + TaoCOde可以省去代码仓库的租建:同时还可以很好的满足小团队之间敏捷开发的需求.接下来详细介绍整个搭建流程. 首先,介绍所用到的工具: 1,Myecli ...
- gdb生成的core文件位置
gdb可以生成core文件,记录堆栈信息,core文件名字是下面这种格式 :core.9488,其中9488是PID 文件位置是当前目录
- 转:使用 SCons 轻松建造程序
转: https://www.ibm.com/developerworks/cn/linux/l-cn-scons/ 在软件项目开发过程中,make 工具通常被用来建造程序.make 工具通过一个被称 ...
- MongoDB与MySQL的插入性能测试【转】
1.1 MongoDB的简单介绍 在当今的数据库市场上,MySQL无疑是占有一席之地的.作为一个开源的关系型数据库,MySQL被大量应用在各大网站后台中,承担着信息存储的重要作用.2009年,甲骨文 ...