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. ny49 开心的小明

    开心的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 小明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他 ...

  2. innerHTML属性,outerHTML属性,textContent属性,innerText属性区别详解

    innerHTML属性用来读取或设置某个节点内的HTML代码. outerHTML属性用来读取或设置HTML代码时,会把节点本身包括在内. textContent属性用来读取或设置节点包含的文本内容. ...

  3. singer页面点击歌手singer是跳转到singer-detail的设置

    1.创建components/singer-detail/singer-detail.vue 2.配置动态路由: { path: ':id', name:'singer-detail', compon ...

  4. LeetCode: Max Points on a Line 解题报告

    Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...

  5. 创建一个动态链接库 (DLL),使用VS2010

    在本演练中,您将创建一个动态链接库 (DLL),其中包含可供其他应用程序使用的有用例程.使用 DLL 是一种重用代码的绝佳方式.您不必在自己创建的每个程序中重新实现这些例程,而只需对这些例程编写一次, ...

  6. [uart]UART中的硬件流控RTS与CTS

    转自:http://blog.csdn.net/zeroboundary/article/details/8966586 在RS232中本来CTS 与RTS 有明确的意义,但自从贺氏(HAYES ) ...

  7. ansible 的user模块

    user模块与group模块 user模块是请求的是useradd, userdel, usermod三个指令,goup模块请求的是groupadd, groupdel, groupmod 三个指令. ...

  8. 05 Oracle process

    本章提要----------------------------------------------所有的 process 都是在 PGA 内(memory)server process: 与 cli ...

  9. R语言--saprkR基本使用

    1.在sparkR的shell中交互式使用 sparkR --masterspark://10.130.2.20:7077 sparkR --masterlocal[6] #sparkR --mast ...

  10. 【F12】网络面板

    使用网络面板了解请求和下载的资源文件并优化网页加载性能 (1)网络面板基础 测量资源加载时间 使用 Network 面板测量您的网站网络性能. Network 面板记录页面上每个网络操作的相关信息,包 ...