HDU 4956 Poor Hanamichi(暴力)——BestCoder Round #5

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+An1×10n1++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+An1×10n1++A2×102+A1×101+A0)mod11
An×(1)n+An1×(1)n1++A2A1+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
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-10
using 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;
}
菜鸟成长记

你可能感兴趣的:(ACM,暴力)