杭电多校Day7 1001 A + B = C

思路

我们让a或b和c的长度相同,然后c减去a或b,那么得到的就一定包含b或a,hash检测一下就好了
比如a=12223 b=1112222 c=123342222
那么我们就可以变成a=122230000 b=11122222 c=123342222
这样 c-a=11122222显然是包含b的,不过我们要先去掉abc的后导零

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll base = 10;
const ll mod = 386910137;
int x,y,z,_1,_2,_3;
bool solve(string a,string b,string c)
{
    _1 = _2 = _3 = 0;
    if (a.size() < c.size())
    {
        int r = c.size()-a.size();
        for (int i = 0;i < r;i ++)
            a += '0',++_1;
    }
    else
    {
        int r = a.size()-c.size();
        for (int i = 0;i < r;i ++)
            c += '0',++_3;
    }
    ll cnt1 = 0,cnt2 = 0,f = 0;
    if (a>c) c += '0',++_3,cnt1 = c[0]-'0',f = 1;
    for (int i = 0;i < a.size();++ i)
    {
        cnt1 = cnt1*base;
        if (c[i+f] < a[i]) cnt1 = cnt1-base+10+c[i+f]-a[i];
        else cnt1 += c[i+f]-a[i];
        cnt1 %= mod;
    }
    for (int i = 0;i < b.size();i ++)
        cnt2 = (cnt2*base+b[i]-'0')%mod;
    if (cnt1 == cnt2) return 1;
    if (c.size() < b.size() || c < a) return 0;
    for (int i = 0;i < c.size()-b.size();++ i)
    {
        ++ _2;
        cnt2 = cnt2 * base % mod;
        if (cnt1==cnt2) return 1;
    }
    return 0;
 }
int main()
{
    ios::sync_with_stdio(false);
    string a,b,c,s;
    int t;
    cin>>t;
    while (t --)
    {
        x = y = z = 0;
        cin>>a>>b>>c;
        int index=-1;
        for (int i = a.size()-1;;--i,--x)
            if (a[i] != '0')
            {
                index = i;
                break;
            }
        s.clear();
        for (int i = 0;i <= index;++i)
            s += a[i];
        a = s;
        index = -1;
        s.clear();
        for (int i = b.size()-1;;--i,-- y)
            if (b[i] != '0')
            {
                index = i;
                break;
            }
        for (int i = 0;i <= index;i ++)
            s += b[i];
        b = s;
        index = -1;
        s.clear();
        for (int i = c.size()-1;;--i,-- z)
            if (c[i] != '0')
            {
                index = i;
                break;
            }
        for (int i = 0;i <= index;i ++)
            s += c[i];
        c = s;
        if (solve(a,b,c))
        {
            x += _1,y += _2,z += _3;
            if (x < 0 || y < 0 || z < 0)
            {
                int w = -min({x,y,z});
                x += w,y += w,z += w;
            }
            cout<<x<<' '<<y<<' '<<z<<'\n';
        }
        else if (solve(b,a,c))
        {
            x += _2,y += _1,z += _3;
            if (x < 0 || y < 0 || z < 0)
            {
                int w = -min({x,y,z});
                x += w,y += w,z += w;
            }
            cout<<x<<' '<<y<<' '<<z<<'\n';
        }
        else cout<<-1<<'\n';
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值