题目大意:给出三个十进制下的数字 A ,B ,C ,问是否存在一个 k ,满足在 k 进制下有 A * B = C
题目分析:需要观察出进制与 A * B 和 C 的大小之间具有单调性,如果进制较小的话,那么进位自然就会变多,从而使得位数边长,整体数字就会变大,反之亦然,所以据此,可以直接二分答案,用一个 check 进行检查,需要注意的是答案会爆 long long,所以整体用 __int128 进行计算即可,二分的下限设置为所有数字的最大值 + 1,上限设置为 1e20
代码:
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;
typedef __int128 LL;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e3+100;
template <typename T>inline void read(T& t){
t=0;
register char ch=getchar();
while(!('0'<=ch&&ch<='9')){
if(ch=='-') t*=-1;
ch=getchar();
}
while(('0'<=ch&&ch<='9')){
t=((t<<1)+(t<<3))+ch-'0';
ch=getchar();
}
}
template <typename T>inline void write(T x){
if(x<0) putchar('-'),x=~(x-1);
int s[50],top=0;
while(x) s[++top]=x%10,x/=10;
if(!top) s[++top]=0;
while(top) putchar(s[top--]+'0');
}
LL a[N],b[N],c[N],cc[N*N];
int la,lb,lc;
LL l=0,r=1e20;
void input(int& n,LL a[])
{
scanf("%d",&n);
n--;
for(int i=n;i>=0;i--)
{
read(a[i]);
l=max(l,a[i]+1);
}
}
int check(int pos)//1:大了,-1:小了
{
if(pos>lc)
return -1;
if(pos<lc)
return 1;
for(int i=pos;i>=0;i--)
{
if(cc[i]>c[i])
return -1;
if(cc[i]<c[i])
return 1;
}
return 0;
}
int cal(LL mid)
{
memset(cc,0,sizeof(cc));
for(int i=0;i<=la;i++)
for(int j=0;j<=lb;j++)
cc[i+j]+=a[i]*b[j];
int pos=0;
while(pos<=la+lb||cc[pos])
{
cc[pos+1]+=cc[pos]/mid;
cc[pos]%=mid;
pos++;
}
return check(pos-1);
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);
input(la,a);
input(lb,b);
input(lc,c);
while(l<=r)
{
LL mid=l+r>>1;
int t=cal(mid);
if(t==0)
{
write(mid);
return 0;
}
else if(t==1)
r=mid-1;
else if(t==-1)
l=mid+1;
}
puts("impossible");
return 0;
}