倒着维护可以取的范围区间,判一下可不可能无解即可。
#include<bits/stdc++.h> #define ll long long using namespace std; const int N=1e5+5; int n,a[N]; ll L,R; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",a+i); L=R=2; for(int i=n;i;i--){ L=(L-1)/a[i]+1,R=R/a[i]; if(L>R){ puts("-1"); return 0;} L*=(ll)a[i],R=(R+1)*(ll)a[i]-1; } printf("%lld %lld\n",&L,&R); return 0; }