Poor Hanamichi

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 743    Accepted Submission(s): 275

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

解题思路:

题意为:存在一个问题求一个区间([l,r])内有多少数满足(偶数位的和-奇数位的和==3)。

Hanamichi提出了它的解法 Answer =  (r + 8) / 11 – (l – 1 + 8) / 11.我们的任务就是找到一个最小的右端点,使得该区间内满足条件的数不满足Hanamichi的解法。

一開始读完题意想的是数位Dp,但后来用暴力搞了.....

代码:

#include <iostream>
using namespace std; long long cal(long long l,long long r)//常规方法求区间内满足条件的数有多少个
{
long long result=0;
for(;l<=r;l++)
{
long long temp=l;
int mul=1;
int re=0;
while(temp)
{
re+=mul*(temp%10);
temp/=10;
mul=(-mul);
}
if(re==3)
result++;
}
return result;
} long long re(long long l,long long r)
{
return (r+8)/11-(l-1+8)/11;
} int main()
{
long long l,r,R;
int t;
cin>>t;
while(t--)
{
cin>>l>>r;
for(R=l;R<=r;R++)//从小到大,输出最小的那个
{
long long result=re(l,R);
if(cal(l,R)!=result)
{
cout<<R<<endl;
break;
}
}
if(R>r)
cout<<-1<<endl;
}
return 0;
}

[BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)的更多相关文章

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Ot ...

  2. Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  3. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  4. [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)

    BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, ...

  5. [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

    Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...

  6. 【HDOJ】4956 Poor Hanamichi

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

  7. BestCoder Round #60/HDU 5505 暴力数学

    GT and numbers 问题描述 给出两个数NN和MM. NN每次可以乘上一个自己的因数变成新的NN. 求最初的NN到MM至少需要几步. 如果永远也到不了输出-1−1. 输入描述 第一行读入一个 ...

  8. BestCoder Round #92 (hdu 6015 6016)

    比赛链接 A题主要是map的使用,比赛的时候问了下队友,下次要记住了 #include<bits/stdc++.h> using namespace std; typedef long l ...

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

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

随机推荐

  1. solr进阶九:solr对数字和单个字符的搜索

    solr中配有分词器,但分词器里面有大写转换为小写的过滤器时,数字就会被过滤.比如123中国ABC.123就会被过滤掉了,所以才查1|12|123都会失败. <Solr 数字字符不能搜索的一个问 ...

  2. 一个div宽度不固定的左右居中效果

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  3. Eclipse中设置文件编码

    如果你在使用某个editor进行开发的话,文件编码就由改editor解决即可 Eclipse中也有这个功能,帮你设置文件的编码,选择Edit->Set Encoding即可 注意,这个选项针对不 ...

  4. Python 下载excel

    上面源码 View:result_list是一个list为数据,当为list时,用enumerate可以获取到list的值和值所在序号 xlsx = openpyxl.Workbook() table ...

  5. poj2018(高精度二分+dp)

    题意:给你n个数,要你在这n个数里面找到一些连续的数,这些数的数量大于等于m,并且他们的平均值在这n个数里面是最大的....... 思路:先把n个数的最大最小值确定,然后二分枚举平均值,对于每一个连续 ...

  6. Oracle主表列表上显示从表字段拼成的字符串

    select t.*,t1.person_names from itil_duty_group t ,( select p.duty_group_id, WMSYS.WM_CONCAT(p.perso ...

  7. Spring事务处理时自我调用的解决方案 嵌套AOP

    开涛的解决方案1 http://jinnianshilongnian.iteye.com/blog/1487235 AopContext.currentProxy() 原理 http://books. ...

  8. R语言实战实现基于用户的简单的推荐系统(数量较少)

    R语言实战实现基于用户的简单的推荐系统(数量较少) a<-c(1,1,1,1,2,2,2,2,3,3,3,4,4,4,5,5,5,5,6,6,7,7) b<-c(1,2,3,4,2,3,4 ...

  9. C++ 递归实现汉诺塔

    C++实现汉诺塔 #include <iostream> using namespace std; void move(int n,char x,char y,char z) { ) { ...

  10. MongoDB(三):MongoDB概念解析

    在上一篇文章中讲解了如何安装MongoDB,这篇文章中讲解一些有关MongoDB的概念. 不管我们要学习什么数据库,都应该学习其中的基础概念,在MongoDB中基本的概念是文档.集合.数据库,下面挨个 ...