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 ...
随机推荐
- leetcode 318. Maximum Product of Word Lengths
传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...
- shell的函数的简单入门
shell的函数的简单入门 语法: function funcname(){} 示例: #!/bin/bash #定义将参数转化为大写的函数 function strToUpper(){ | tr ' ...
- HDU4850 构造一个长度为n的串,要求任意长度为4的子串不相同
n<=50W.(使用26个字母) 构造方法:26个,最多构造出26^4种不同的串,长度最长是26^4+3,大于是输出"impossble",用四维数组判重.每次向前构造一位( ...
- 基于SSH+shiro+solr的家庭记账系统
项目地址: https://github.com/jianghuxiaoao/homeaccount
- Google代码风格配置文件(Java)(IDEA/Eclipse)
官网:http://www.cnblogs.com/EasonJim/p/7837474.html 下载: 安装: IDEA/Eclipse导入相应文件即可. 说明: Google代码风格文件的缩进是 ...
- ubuntu16.04安装mysql5.7.15
1.在官网下载mysql安装包 直接选择第一个下载好了就行 2.进入你的下载文件夹下面 键入命令: tar -xvf mysql-server_5.7.13-1ubuntu16.04_i386.deb ...
- decorate all function in all module
需求: 有package db_api,其下有很多 module 如 plane.py ship.py ufo.py.这些module内定义了方法如 plane.fly(), ship.float() ...
- Invalid regular expression: unmatched parentheses
Unmatched ) in Javascript regular expression您的某些字符串包含错误')'.你需要逃避这个.这是这样做的功能: function escapeRegExp(s ...
- Android eclipse 项目R文件无法生成
这个问题相信非常多人从网上已经搜到不少的解决方式. android 无法生成R文件 是件痛苦的事情.即使有时候你xml文件没有错误,他都无法生成. 针对此问题,我总结下面几个方面的原因. 一.xml本 ...
- 嵌入式学习笔记(综合提高篇 第一章) -- 利用串口点亮/关闭LED灯
1 前言 从踏入嵌入式行业到现在已经过去了4年多,参与开发过的产品不少,有交换机.光端机以及光纤收发器,停车场出入缴费系统,二维码扫码枪,智能指纹锁以及数字IC芯片开发等; 涉及产品中中既有 ...