HDU - 6646 A + B = C hash+思維

A + B = C

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2314    Accepted Submission(s): 550
Special Judge

 

Problem Description

Hi everyone! Welcome to the Stage 7 of this series of training contests. Cuber QQ is the problem settler of this contest; he has prepared 11 problems for you and wishes you to enjoy it. Good luck!

As the first problem of this contest, Cuber QQ thought that it's reasonable to start with an easy one, so he modified the famous A + B problem by a little bit, so that it's easy enough but not that trivial.

Given a,b,c , find an arbitrary set of x,y,z such that a⋅10x+b⋅10y=c⋅10z and 0≤x,y,z≤106.

 

 

Input

The input consists of multiple test cases, starting with an integer t (1≤t≤100), denoting the number of test cases.

For each test case, there are three space-separated integers, a,b,c respectively (1≤a,b,c≤10100000).

 

 

Output

For each test case, please output three space-separated integers x,y,z. If there are multiple solutions, print any of them.

In case there are no solutions, please output −1 as a single line.

 

 

Sample Input


 

3 23 39 62 2 31 51 1 1 1

 

 

Sample Output


 

0 0 0 1 0 0 -1

Hint

HDOJ may give ambiguous feedback due to the compatibility issue of our checker. If you see "System Error", please think of it as "Wrong Answer".

 

 

Source

2019 Multi-University Training Contest 7

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6742 6741 6740 6739 6738 

 

 


 

OJ題號

 HDU - 6646 A + B = C

簡單題意

題意爲求a*10^x+b*10^y=c*10^z滿足公式的任意一組解x,y,z。

正解思路

我們知道a+b的長度只有max(lena,lenb)或者max(lena,lenb)+1兩種情況,所以我們一開始讓abc的長度一樣,分四種種情況枚舉即可:

  • a與c對齊,枚舉爲b的初始長度到增加0後的長度
  • 可能會產生進位,a-1與c對齊,枚舉爲b的初始長度到增加0後的長度
  • 剩下兩種把a與b倒換過來即可

但我們會找到一種反例,1110,100,1120,如果我們補齊11100,10000,11200,是得不到答案的,我們要補齊的111000,100000,112000,我們發現多補0是有好處的,而且我們發現100的00如果去掉一個0的話,那麼可以直接出答案了,去掉它的,相當於增加1110,1120的,所以,我們需要我們把後綴0給補出來,初始的補齊長度:

             len=max(oldlen[3],max(oldlen[1]-suff_0[1],oldlen[2]-suff_0[2])+max(suff_0[1],suff_0[2])+1);

suff_0:後綴0的長度

oldlen:初始長度

 

#include <bits/stdc++.h>
using namespace std;

const int N = 500005;
typedef unsigned long long ULL;
typedef  long long LL;
struct Hash
{
    //下標從1開始
    ULL base,mod,Hashv[N],pw[N];
    //base:131,13331
    //mod:998244353,1e9+7,1e9+9
    int tot;

    init(ULL tbase,ULL tmod)
    {
        tot=0;
        base=tbase;
        mod=tmod;
        pw[0]=1;
    }
    void add(int c)
    {
        tot++;
        Hashv[tot]=(Hashv[tot-1]*base+c)%mod;
        pw[tot]=(pw[tot-1]*base)%mod;
    }
    ULL query(int l,int r)
    {
        return (Hashv[r]-Hashv[l-1]*pw[r-l+1]+mod)%mod;
    }


} h[10];
bool check(Hash &h1,int l1,int r1,Hash &h2,int l2,int r2,Hash &h3,int l3,int r3)
{

    //cout<<l1<<" "<<r1<<"   "<<l2<<" "<<r2<<"   "<<l3<<" "<<r3<<endl;
    //cout<<h1.query(l1,r1)<<" "<<h2.query(l2,r2)<<" "<<h3.query(l3,r3)<<endl<<endl;

    if((h1.query(l1,r1)+h2.query(l2,r2))%h3.mod==h3.query(l3,r3))
        return 1;

    return 0;
}
char str[4][N];
int oldlen[4],nowlen[4];
int suff_0[4];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
    	
        for(int i=1; i<=3; i++)
        {
        	suff_0[i]=0;
            h[i].init(10,1e9+7);
            h[i+3].init(10,998244353);
            scanf("%s",str[i]+1);
            oldlen[i]=nowlen[i]=strlen(str[i]+1);
        }
        for(int i=1; i<=3; i++)
        {

            for(int j=1; j<=oldlen[i]; j++)
            {
                h[i].add(str[i][j]-'0');
                h[i+3].add(str[i][j]-'0');
                
            }
            for(int j=oldlen[i]; j>=1; j--)
            {
            
                if(str[i][j]!='0')
                    break;
                suff_0[i]++;
            }
        }

        int len=max(oldlen[3],max(oldlen[1]-suff_0[1],oldlen[2]-suff_0[2])+max(suff_0[1],suff_0[2])+1);
        //cout<<suff_0[1]<<" "<<suff_0[2]<<endl;
        LL x,y,z=len-oldlen[3];
        for(int i=1; i<=3; i++)
        {
            for(int j=oldlen[i]+1; j<=len; j++)
            {
                h[i].add(0);
                h[i+3].add(0);
            }
        }
        int flag=0;
         for(int i=oldlen[2]; i<=len; i++)
        {
            if(check(h[1],1,len,h[2],1,i,h[3],1,len)&&check(h[4],1,len,h[5],1,i,h[6],1,len))
            {
                y=i-oldlen[2];
                x=len-oldlen[1];
                flag=1;
                break;

            }

            if(check(h[1],1,len-1,h[2],1,i,h[3],1,len)&&check(h[4],1,len-1,h[5],1,i,h[6],1,len))
            {
                y=i-oldlen[2];
                x=len-1-oldlen[1];
                flag=1;
                break;

            }
        }

       

        if(flag==1)
        {
            printf("%lld %lld %lld\n",x,y,z);
            continue;
        }
         for(int i=oldlen[1]; i<=len; i++)
        {
            //cout<<i<<" "<<len<<endl;
            if(check(h[1],1,i,h[2],1,len,h[3],1,len)&&check(h[4],1,i,h[5],1,len,h[6],1,len))
            {
                x=i-oldlen[1];
                y=len-oldlen[2];
                flag=1;
                break;

            }


            if(check(h[1],1,i,h[2],1,len-1,h[3],1,len)&&check(h[4],1,i,h[5],1,len-1,h[6],1,len))
            {

                x=i-oldlen[1];
                y=len-1-oldlen[2];
                flag=1;
                break;

            }
        }

       
        if(flag==1)
        {
            printf("%lld %lld %lld\n",x,y,z);
            continue;
        }
        printf("-1\n");


    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章