題意:給出a琐鲁,b,c人灼。求x围段,y,z滿足 投放。
題解:先把a(bǔ)奈泪,b,c去掉末尾的0得到A, B, C灸芳。這樣我們要解的方程就是: 涝桅。
- 如果
, 那么有
烙样, 因?yàn)锳和B的末尾都不是0
- 如果
冯遂, 那么
和
中至少有一個(gè)為0,因?yàn)镃的末位不是0
- 根據(jù)末位判斷一下哪一個(gè)是0就好了
但是高精度又慢又寫(xiě)起來(lái)很麻煩误阻。所以我們可以采用hash的想法债蜜,對(duì)一個(gè)大質(zhì)數(shù)取mod然后加減乘除判斷相等都很容易
btw, 這個(gè)題對(duì)java相當(dāng)不友好晴埂,用java自帶的BigInteger根本過(guò)不了,比賽的時(shí)候浪費(fèi)了好幾個(gè)小時(shí)去調(diào)高精度的板子寻定。不過(guò)取模之后就能大大節(jié)省時(shí)間
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#define int int64_t
using namespace std;
using pii = pair<int, int>;
const int mod = 355542559;
const int maxn = 1e6 + 10;
int bin(int x,int n,int MOD){
int ret = MOD != 1;
for (x %= MOD; n; n >>= 1,x=x*x%MOD){
if(n&1)
ret = ret * x % MOD;
}
return ret;
}
inline int get_inv(int x,int p){
return bin(x, p - 2, p);
}
string remove_zero(const string &a)
{
int res = 0;
for (int i = a.length() - 1; i >= 0; i--)
{
if (a[i] == '0')
res++;
else
break;
}
return a.substr(0, a.length() - res);
}
int powten(int val){
static vector<pii> vec;
if(vec.size()==0){
for (int i = 1, cnt = 0; cnt < maxn;cnt++,i=i*10%mod){
vec.emplace_back(i, cnt);
}
sort(vec.begin(), vec.end());
}
auto it = lower_bound(vec.begin(), vec.end(), make_pair(val, 0ll));
if(it->first==val){
return it->second;
}
return -1;
}
int last_digital(const string &str)
{
int len = str.length() - 1;
return str[len] - '0';
}
int go(const string& str){
int res = 0;
for(auto ch:str){
res = (res * 10 + ch - '0') % mod;
}
return res;
}
int32_t main()
{
ios::sync_with_stdio(false);
int _;
cin >> _;
while (_--)
{
string a, b, c;
cin >> a >> b >> c;
string za1 = remove_zero(a);
string zb1 = remove_zero(b);
string zc1 = remove_zero(c);
int ra = a.length() - za1.length();
int rb = b.length() - zb1.length();
int rc = c.length() - zc1.length();
auto za = go(za1);
auto zb = go(zb1);
auto zc = go(zc1);
int m = last_digital(za1);
int n = last_digital(zb1);
int p = last_digital(zc1);
int x = (int)-1e9, y = (int)-1e9, z = (int)-1e9;
if (m == p)
{
auto tmp = (zc - za + mod) % mod;
int pow = powten(tmp * get_inv(zb,mod) % mod);
if (pow >= 0)
{
x = rb + rc - pow;
y = ra + rc;
z = ra + rb - pow;
}
}
if (n == p)
{
auto tmp = (zc - zb + mod) % mod;
int pow = powten(tmp * get_inv(za, mod) % mod);
if (pow >= 0)
{
x = rb + rc;
y = ra + rc - pow;
z = ra + rb - pow;
}
}
auto tmp = za + zb;
int pow = powten(tmp * get_inv(zc, mod) % mod);
if (pow >= 0)
{
x = rb + rc - pow;
y = ra + rc - pow;
z = ra + rb;
}
if (x == (int)-1e9)
{
cout << -1 << endl;
}
else
{
int _min = min(x, min(y, z));
x -= _min;
y -= _min;
z -= _min;
int _max = max(x, max(y, z));
if (_max > int(1e6))
{
cout << -1 << endl;
}
else
cout << x << " " << y << " " << z << endl;
}
}
}