题目链接: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

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 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.
 
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
3
3 4
2 50
7 83
 
Sample Output
-1
-1
80
 
Source

题意:

首先给出一个范围 [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(数学题)的更多相关文章

  1. [BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)

    Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)

    HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=59 ...

  3. 【HDOJ】4956 Poor Hanamichi

    基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. #include <cstdio> int f(__int64 x) { int i, sum; i = ...

  4. HDU - 5996 树上博弈 BestCoder Round #90

    就是阶梯NIM博弈,那么看层数是不是奇数的异或就行了: #include<iostream> #include<cstdio> #include<algorithm> ...

  5. HDU 5904 - LCIS (BestCoder Round #87)

    HDU 5904 - LCIS [ DP ]    BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...

  6. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  7. hdu 5643 BestCoder Round #75

    King's Game  Accepts: 249  Submissions: 671  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 6 ...

  8. hdu 5641 BestCoder Round #75

    King's Phone  Accepts: 310  Submissions: 2980  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  9. hdu 5636 搜索 BestCoder Round #74 (div.2)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

随机推荐

  1. ios 调试过程捕获异常Stack 信息

    在AppDelegate,定义方法 void catchExceptionHandler(NSException *exception) { NSLog(@"CRASH: %@", ...

  2. IOS推送通知測试工具PushMeBaby

    下载了PushMeBaby在xcode5里中不能使用.类库变了.须要加入Carbon.framework库.在引用的地方改成: #include <Carbon/Carbon.h>.程序就 ...

  3. curl Error : maximum redirects followed , 这种问题的一种原因 .

    在stack overflow 上查找到有些网站上需要返回一些cookie的,所以当我们curl当相应的网站时,必须要将返回的cookie保存起来. $cookie = tempnam (" ...

  4. sql语句单据编号生成防并发

    有用户反馈说发现重复单据号,检查发现以下单据号被分配给了不同的两个职工 系统中使用语句exec GetNewOrderNumber 'pwgnumber','PWG',1, @pwg_number o ...

  5. C#协变与逆变

    http://zh.wikipedia.org/wiki/%E5%8D%8F%E5%8F%98%E4%B8%8E%E9%80%86%E5%8F%98 协变与逆变是程序设计语言中的类型系统的一对概念.类 ...

  6. iOS的isnan函数

    假设一个数是一个确定的数.那它就不是nan值 假设一个数是无穷大,无穷小.那它就是nan值 我试着打印了下面的值是不是nan值 if (isnan(1)) { DLog(@"1是NAN&qu ...

  7. Tomcat控制台输出

    在Tomcat中,默认将终端输出信息输出到: $CATALINA_HOME/logs/catalina.out  其中$CATALINA_HOME是tomcat的安装目录. tomcat启动后,该文件 ...

  8. spring boot 打包报错

    [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.RELEASE:repac ...

  9. [转]sql语句优化原则

    性能不理想的系统中除了一部分是因为应用程序的负载确实超过了服务器的实际处理能力外,更多的是因为系统存在大量的SQL语句需要优化. 为了获得稳定的执行性能,SQL语句越简单越好.对复杂的SQL语句,要设 ...

  10. 测试ssh框架中hibernate的事务

    <!-- 配置切面 --> <aop:config> <aop:pointcut expression="execution(* com.xxx.lobs.ma ...