题意:
给出三个数a , b , c的每位的数,问在几进制下a ∗ b = = c
思路:
可以看出是有单调性的,如果进制数较小的话,那么对于相同的乘积来说进位数也多,长度更长。所以如果对于某个进制a ∗ b < c
由于d i < = 2 30,所以会爆long,用高精度模拟又很容易t l e,所以应该用_ _ i n t 128
代码:
#include<bits/stdc++.h> using namespace std; typedef __int128 ll; typedef pair<int, int>PII; inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;} inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');} #define read read() #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;} const int maxn=2e5+7,inf=0x3f3f3f3f; ll na,nb,nc,a[1100],b[1100],c[1100]; ll tmpc[2100]; /* if(sa*sb==sc) return 1; else if(sa*sb>sc) return 3; return 2; */ int check(ll mid){ ll las=0; int flag=1; for(int i=0;i<nc;i++){ ll num=(las+tmpc[i])%mid; las=(las+tmpc[i])/mid; if(num==c[i]) continue; else if(num>c[i]) flag=3; else if(num<c[i]) flag=2; } return flag; } int main(){ ll l=0,r=(1ll<<61),ans=-1; na=read; for(int i=na-1;i>=0;i--) a[i]=read,l=max(l,a[i]+1); nb=read; for(int i=nb-1;i>=0;i--) b[i]=read,l=max(l,b[i]+1); nc=read; for(int i=nc-1;i>=0;i--) c[i]=read,l=max(l,c[i]+1); for(int i=0;i<na;i++) for(int j=0;j<nb;j++) tmpc[i+j]+=(a[i]*b[j]); while(l<=r){ ll mid=(l+r)/2,t=check(mid); if(t==1){ ans=mid;break; } else if(t==2) r=mid-1; else l=mid+1; // cout<<mid<<" "<<t<<endl; } if(ans==-1) puts("impossible"); else write(ans); return 0; }