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 ...
随机推荐
- 关于事件委托和时间冒泡(以及apply和call的事项)
搜索事件委托和事件冒泡,网上一大堆乱七八糟的解释,当然意思都对,没毛病. but,真的无聊. 事件冒泡:事件会从点击的元素开始依次向上流出,直到html,遇见事件监听则执行. 事件委托:原因——父元素 ...
- 【网摘】sql 语句修改字段名称以及字段类型
网上摘抄,备份使用: 修改字段名: 下例将表 customers 中的列 contact title 重命名为 title. EXEC sp_rename 'customers.[contact ti ...
- mysql报错Packet for query is too large (12238 > 1024). You can change this value
今天将项目部署到linux服务器的时候莫名其妙的报一些错误,可是在本地啥错没有,通过实时查看tomcat 的日志之后发现报错是: 实时查看日志: .先切换到:cd usr/local/tomcat5/ ...
- mongodb的入门学习
mongodb的入门学习 简介: MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库 ...
- Codeforces 665C Simple Strings【暴力,贪心】
题目链接: http://codeforces.com/contest/665/problem/C 题意: 改变最少的字符,使得最终序列无相同的连续的字符. 分析: 对每一个与前一个字符相同的字符,枚 ...
- Nginx常用命令(启动/重启/停止/测试配置文件/重新加载配置文件)
Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的. Nginx 的参数包括有如下几个: 使用: /usr/local/ngin ...
- J粒子发现40周年-丁肇中中科院讲座笔记
J粒子发现40周年-丁肇中中科院讲座笔记 华清远见2014-10-18 北京海淀区 张俊浩 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXVuZm ...
- Linux Shell高级技巧
Linux Shell高级技巧(一) http://www.cnblogs.com/stephen-liu74/archive/2011/12/22/2271167.html一.将输入信息转换为大写字 ...
- Donser Online Judge 完成运行使命~
复试成功完成~ 2018年网研机考难度不大,仍然有些遗憾,前两题水题后两个题纯暴力 排行榜 排名 用户 题数 罚时 A B C D retest2018_INT246 (INT246) (+) (+) ...
- (C++ STL)list的实现
#include <iostream> using namespace std; //採用迭代器和空间配置器所实现的双向链表的基本功能 template<class _Ty,clas ...