HDU 4956 Poor Hanamichi(暴力)——BestCoder Round #5
来源:互联网 发布:家用中央空调知乎 编辑:程序博客网 时间:2024/06/06 03:30
Poor Hanamichi
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
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 areA1,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.
A integer X can be represented in decimal as:
The odd dights are
Hanamichi comes up with a solution, He notices that:
So X mod 11
=
=
= 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.
Input
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 )
Output
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.
Sample Input
33 42 507 83
Sample Output
-1-180
Source
BestCoder Round #5
出题人的解题思路:
直接从l扫描到r,发现的第一个不满足性质,却满足X mod 11 = 3的数(比如80就是这样的数)就是我们的答案。可以证明扫描的距离不超过10000必然会有答案。其实,说白了,题目就是让我们找到区间[l,r]内最小的一个数X,它必须满足如下条件:
①X mod 11 = 3;
②X的偶数位之和减去奇数位之和不等于3
若区间内无这样的数,则输出-1
看了题目中一大串证明,我们可以看出他的证明大致上是没有问题的,但问题存在于忽略了一点小细节,即X mod 11 = 3的数X不一定是偶数位之和-奇数位之和=3,也有可能是X的奇数位之和-偶数位之和=8
我们都知道的一点是,被11整除余3的数,你减去3或者加上8都是可以被11整除的,所以Hanamichi的解决方案的bug就在这里
知道这一点之后,我们便可以枚举区间内的每个数,判断是不是满足所给条件即可。
唯一有点疑问的就是如何证明扫描的距离不超过10000必然会有答案,这一点还有待大家来帮我解答,望分享
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 2;const int inf = 1000000000;const int mod = 258280327;bool check(__int64 x){ if(x%11==3) { int k=0,s[N]={0}; while(x) { s[k]+=x%10; k^=1; x/=10; } if(s[0]-s[1]!=3) return true; } return false;}int main(){ int t; __int64 l,r; scanf("%d",&t); while(t--) { scanf("%I64d%I64d",&l,&r); while(l<=r) { if(check(l)) break; l++; } l>r?puts("-1"):printf("%I64d\n",l); } return 0;}菜鸟成长记
0 0
- HDU 4956 Poor Hanamichi(暴力)——BestCoder Round #5
- hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)
- [BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)
- hdu 4956 ( BestCoder #5 1001) Poor Hanamichi
- hdu 4956 Poor Hanamichi(bestcoder Round 6 1001)
- hdu 4956 Poor Hanamichi(暴力)
- HDU—— 4956 Poor Hanamichi
- HDU 4956 Poor Hanamichi
- HDU 4956/BC 5A Poor Hanamichi
- hdu 4956 Poor Hanamichi(枚举)
- Round #5 1001 Poor Hanamichi(原来这么简单啊)
- 【CUGBACM15级BC第五场 A】hdu 4956 Poor Hanamichi
- hdu 4956 Poor Hanamichi(BC.R#5) 读懂了题意就是水题/坑- -比赛中居然没有过
- HDU 5147 Ferries Wheel(暴力 数组)——BestCoder Valentine's Day Round
- HDU 5479 Scaena Felix(暴力)——BestCoder Round #57(div.2)
- HDU 5522 Numbers(暴力)——BestCoder Round #61(div.2)
- HDU 5567 sequence1(暴力)——BestCoder Round #63(div.2)
- HDU 5778 abs(暴力枚举)——BestCoder Round #85 1003
- Android实现天气预报温度/气温折线趋势图
- 消除应用启动时的黑屏现象
- spring中context:property-placeholder/元素
- sublime text3 加入右键编辑
- Spring-BeanFactoryPostProcessor
- HDU 4956 Poor Hanamichi(暴力)——BestCoder Round #5
- liferay6.2教程之把 liferay视为协同平台
- HDU 1247 Hat’s Words(字典树)
- 第81讲:Scala中List的构造是的类型约束逆变、协变、下界详解
- 1、restlet 2.3 用户指南(一)概要
- 【日常学习】【划分DP】codevs1017 乘积最大题解
- jQuery基础----01jQuery简介及语法,jQuery的基本语法
- 第82讲:Scala中List的ListBuffer是如何实现高效的遍历计算的?
- css3,可以为一个元素添加多个动画