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

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 (数学题)

时间: 2024-11-08 22:19:02

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

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 Problem Description Hanamichi is taking part in

HDU 4956 Poor Hanamichi

HDU 4956 Poor Hanamichi 题目链接 思路:直接从l往上找判断即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; int t; ll l, r; bool judge(ll num) { ll flag = 1; ll ans = 0; whi

hdu 4956 Poor Hanamichi 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956(它放在题库后面的格式有一点点问题啦,所以就把它粘下来,方便读者观看) 题目意思:给出一个范围 [l, r] 你, 问是否能从中找到一个数证明 Hanamichi’s solution 的解法是错的. Hanamichi’s solution 是这样的: 对于某个数 X,从右往左数它的每一位数字(假设第一位是从0开始数).它 偶数位的数字之和 -  奇数位的数字之和  = 3  而且 这个 X

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

Bestcoder round #65 &amp;&amp; hdu 5592 ZYB&#39;s Premutation 线段树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 74 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutat

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

[BestCoder Round #4] hdu 4932 Miaomiao&#39;s Geometry (贪心)

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 92 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

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

Task schedule Problem Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行. 机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求. Input 输入的第一行包含一个整数T, 表示一共有T组测试数据. 对于每组测试数据: 第一行是两个数字n, m,表示工作表里面有n个任务,