hdu4956 Poor Hanamichi

解决暴力的直接方法。一个直接的推论x%11方法。

打表可以发现,以解决不同的情况都不会在很大程度上会出现。

所以从l暴力开始枚举。找到的第一个错误值输出要。

如果它超过r同样在美国发现-1.


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:16777216")
#define eps 1e-6
#define ll long long
using namespace std;

bool judge(ll x)
{
    ll odd=0,even=0,flag=0;
    while(x>0)
    {
        if(flag)
        {
            odd+=x%10;
            flag=0;
        }
        else
        {
            even+=x%10;
            flag=1;
        }
        x/=10;
    }
    return (even-odd==3);
}

int main()
{
    ll l,r;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cin>>l>>r;
        while(l<=r)
        {
            if((l%11==3)^judge(l))
                break;
            l++;
        }
        if(l>r) printf("-1
");
        else cout<<l<<endl;
    }
    return 0;
}


版权声明:本文博客原创文章。博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/hrhguanli/p/4643091.html