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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值