hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956
Poor Hanamichi
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7 Accepted Submission(s): 4
Hanamichi is taking part in a programming contest, and he is assigned to solve a special problem as follow: Given a range [l, r] (including l and r), find out how many numbers in this range have the property: the sum of its odd digits is smaller than the sum of its even digits and the difference is 3.
A integer X can be represented in decimal as:
X=An×10n+An−1×10n−1+…+A2×102+A1×101+A0
The odd dights are A1,A3,A5… and A0,A2,A4… are even digits.
Hanamichi comes up with a solution, He notices that:
102k+1 mod 11 = -1 (or 10), 102k mod 11 = 1,
So X mod 11
= (An×10n+An−1×10n−1+…+A2×102+A1×101+A0)mod11
= An×(−1)n+An−1×(−1)n−1+…+A2−A1+A0
= sum_of_even_digits – sum_of_odd_digits
So he claimed that the answer is the number of numbers X in the range which satisfy the function: X mod 11 = 3. He calculate the answer in this way :
Answer = (r + 8) / 11 – (l – 1 + 8) / 11.
Rukaw heard of Hanamichi’s solution from you and he proved there is something wrong with Hanamichi’s solution. So he decided to change the test data so that Hanamichi’s solution can not pass any single test. And he asks you to do that for him.
You are given a integer T (1 ≤ T ≤ 100), which tells how many single tests the final test data has. And for the following T lines, each line contains two integers l and r, which are the original test data. (1 ≤ l ≤ r ≤ 1018)
You are only allowed to change the value of r to a integer R which is not greater than the original r (and R ≥ l should be satisfied) and make Hanamichi’s solution fails this test data. If you can do that, output a single number each line, which is the smallest R you find. If not, just output -1 instead.
3
3 4
2 50
7 83
-1
-1
80
题意:
首先给出一个范围 [l, r],问是否能从中找到一个数证明 Hanamichi’s solution 的解法(对于某个数 X,偶数位的数字之和 - 奇数位的数字之和 = 3 并且 这个 X 满足 X mod 11 = 3 )是错的。
也就是在范围里寻找是否存在不能同一时候满足:①偶数位的数字之和
- 奇数位的数字之和 = 3。 ②X mod 11 = 3。
代码例如以下:
#include <cstdio>
typedef __int64 LL; bool Judge(LL tt)
{
LL sumo = 0, sume = 0;
LL i = -1;
while(tt)
{
i++;
LL t = tt%10;
if(i%2)
sumo += t;
else
sume += t;
tt /= 10;
}
if(sume - sumo == 3)
return true;
return false;
}
int main()
{
int T;
LL l, r;
LL i, j;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&l,&r);
for(i = l; ; i++)
{
if(i%11 == 3)
break;
}
for(j = i; j <= r; j+=11)
{
if(!Judge(j))
break;
}
if(j > r)
printf("-1\n");
else
printf("%I64d\n",j);
}
return 0;
}
hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)的更多相关文章
- [BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)
Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)
HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=59 ...
- 【HDOJ】4956 Poor Hanamichi
基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. #include <cstdio> int f(__int64 x) { int i, sum; i = ...
- HDU - 5996 树上博弈 BestCoder Round #90
就是阶梯NIM博弈,那么看层数是不是奇数的异或就行了: #include<iostream> #include<cstdio> #include<algorithm> ...
- HDU 5904 - LCIS (BestCoder Round #87)
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- hdu 5643 BestCoder Round #75
King's Game Accepts: 249 Submissions: 671 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6 ...
- hdu 5641 BestCoder Round #75
King's Phone Accepts: 310 Submissions: 2980 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- hdu 5636 搜索 BestCoder Round #74 (div.2)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
随机推荐
- Android startActivities()的使用
startActivities()和startActivity类似,也是界面跳转: Intent[] intents = new Intent[2]; intents[0] = new Intent( ...
- TensorFlow学习笔记 速记1——tf.nn.dropout
tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None,name=None) 上面方法中常用的是前两个参数: 第一个参数 x:指输入: 第二个 ...
- 使用CSS3实现响应式标题全屏居中和站点前端性能
要实现标题全屏居中(同一时候在垂直和水平方向居中).有若干种方法,包含使用弹性布局.表格单元.绝对定位和自己主动外边距等. 全屏居中 当中眼下比較流行也比較easy理解的方法是使用绝对定位+偏移实现. ...
- MySQL 慢查询日志(Slow Query Log)
同大多数关系型数据库一样.日志文件是MySQL数据库的重要组成部分.MySQL有几种不同的日志文件.通常包含错误日志文件,二进制日志,通用日志.慢查询日志.等等.这些日志能够帮助我们定位mysqld内 ...
- 用brew安装gcc48
由于mac自带的gcc的版本过低,因此想自己装一个新的gcc. 在网上搜索了一圈后发现用brew install安装比较简单,但可能由于本地的brew有冲突,因此网上的攻略都没有效果. 通过在gith ...
- vivado与modelsim的联合仿真(二)
最近在做Zynq的项目,曾经尝试使用ISE+PlanAhead+XPS+SDK组合和Vivado+SDK来搭建工程,使用中发现前者及其不方便后者有诸多不稳定.近期得闻Xilinx退出Vivado20 ...
- OCR 即 光学字符识别
OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗.亮的模式确定其形状,然后用字符识别方法将形状翻译 ...
- 使用SVN管理unityproject
我们的项目使用SVN管理.这几天遇到了几个问题,攻克了一下.顺便做了一个总结. 1.关于使用SVN管理unity项目的一些设置和说明 首先在unity中进行两部操作:Edit->Proje ...
- string::find_last_of
今天在代码中用到string的这个方法,一不小心就用错了. 这是http://www.cplusplus.com/关于这个方法的解释. Find character in string from th ...
- C#中基本类型占用字节数
bool -> System.Boolean (布尔型,其值为 true 或者 false) byte -> System.Byte (字节型,占 1 字节,表示 8 位正整数,范围 0 ...