(長期更新,補完爲止)
2-G 判正誤
題意:https://ac.nowcoder.com/acm/contest/3003/G
T組數據。
判斷a^d+b^e+c^f是否等於g。
−1e9≤a,b,c,g≤1e9, 0≤d,e,f≤1e9。保證不會出現指數和底數同爲 0 的情況。
思路:
硬算會TLE或MLE。
快速冪取模,爲了增加過題概率,多取幾個模數判斷。
代碼:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int n=6;
const ll mod[n]={998244353, 999998641, 100000007, 1000000007, 1000000009, 1000000103};
ll qmi(ll a,ll b,ll mod){
ll res=1;
while(b){
if(b&1) res=res*a%mod;
b>>=1;
a=a*a%mod;
}
return res;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
ll a,b,c,d,e,f,g;
scanf("%lld%lld%lld%lld%lld%lld%lld",&a,&b,&c,&d,&e,&f,&g);
string ans="Yes";
for(int i=0;i<n;++i)
if( ((qmi(a,d,mod[i])+qmi(b,e,mod[i]))%mod[i]+qmi(c,f,mod[i]))%mod[i]!=g%mod[i] ) {ans="No";break;}
cout<<ans<<endl;
}
}
下面是TLE的java大數代碼。寫個java不容易,所以代碼也貼在這裏......
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int t=in.nextInt();
for(int Case=0;Case<t;++Case){
BigInteger a=in.nextBigInteger(),b=in.nextBigInteger(),c=in.nextBigInteger();
int d=in.nextInt(),e=in.nextInt(),f=in.nextInt();
BigInteger g=in.nextBigInteger();
BigInteger ans=((a.pow(d)).add(b.pow(e))).add(c.pow(f));
if(ans.equals(g)) System.out.println("Yes");
else System.out.println("No");
}
}
}