結果

問題 No.577 Prime Powerful Numbers
ユーザー ryoissyryoissy
提出日時 2017-10-14 02:04:09
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,460 bytes
コンパイル時間 1,191 ms
コンパイル使用メモリ 146,336 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 05:09:32
合計ジャッジ時間 7,245 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 67 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 543 ms
4,376 KB
testcase_04 AC 52 ms
4,380 KB
testcase_05 AC 1,236 ms
4,376 KB
testcase_06 AC 687 ms
4,380 KB
testcase_07 AC 1,852 ms
4,376 KB
testcase_08 AC 481 ms
4,380 KB
testcase_09 AC 397 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <time.h>
#define INF 1000000000LL
#define LUG 1000000000LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
std::random_device rd;
std::mt19937 mt(rd());
ll rdn[201];

ll num[3],num2[3],num3[3];

bool C(ll v,ll tgt,int po){
	ll vv=1;
	for(int i=0;i<po;i++){
		memset(num,0,sizeof(num));
		memset(num2,0,sizeof(num2));
		memset(num3,0,sizeof(num3));
		num[0]=vv%LUG;
		num[1]=vv/LUG;
		num2[0]=v;
		for(int i=0;i<2;i++){
			for(int j=0;j<1;j++){
				num3[i+j]+=num[i]*num2[j];
			}
		}
		for(int i=0;i<2;i++){
			num3[i+1]+=num3[i]/LUG;
			num3[i]=num3[i]%LUG;
		}
		if(num3[2]>=1LL)return false;
		vv=num3[1]*LUG+num3[0];
		if(vv>tgt)return false;
	}
	return vv<=tgt;
}

ll mulmod(ll a,ll b,ll m){
	if(b<a)swap(a,b);
	ll ans=0;
	while(a!=0){
		if(a&1)ans=(ans+b)%m;
		a>>=1;
		b=(b<<1)%m;
	}
	return ans;
}

ll mod_pow(ll x,ll n,ll mod){
	ll res=1;
	while(n>0){
		if(n&1LL)res=mulmod(res,x,mod);
		x=mulmod(x,x,mod);
		n>>=1;
	}
	return res;
}

bool calc(ll v,ll tgt,int po){
	ll ans=1;
	while(po>0){
		if(po&1LL)ans=ans*v;
		v=v*v;
		po>>=1;
	}
	//printf("%lld %lld %lldde\n",ans,tgt,tgt-ans);
	if(ans<tgt)return false;
	if(ans>tgt)return false;
	return true;
}

bool is_prime(ll val){
	if(val==1LL)return false;
	if(val==2LL)return true;
	if(val==3LL)return true;
	if(val%6LL!=1 && val%6LL!=5)return false;
	for(int i=0;i<30;i++){
		ll v=rdn[i]%val;
		if(v==0LL)v=1;
		if(mod_pow(v,val-1LL,val)!=1LL)return false;
	}
	return true;
}

bool check(ll val,int po){
	if(po==1)return is_prime(val);
	else{
		ll l=3,r=min(val,INF);
		if(po==3)r=min(val,1000001LL);
		if(po==4)r=min(val,100001LL);
		if(po==5)r=min(val,10001LL);
		if(po==6)r=min(val,1001LL);
		if(po>6)r=min(val,1000LL);
		if(po>10)r=min(val,100LL);
		while(l+1LL<r){
			ll mid=(l+r)/2LL;
			if(C(mid,val,po))l=mid;
			else r=mid;
		}
		//printf("%lld %lld %d\n",l,val,po);
		if(calc(l,val,po) && is_prime(l))return true;
		else return false;
	}
}

int main(void){
	int q;
	for(int i=0;i<50;i++){
		rdn[i]=rd();
	}
	scanf("%d",&q);
	for(int ub=0;ub<q;ub++){
		ll n;
		scanf("%lld",&n);
		if(n==2LL){
			printf("No\n");
		}else if(n%2LL==0){
			printf("Yes\n");
		}else{
			ll tw=2;
			bool flag=false;
			for(int j=0;j<59;j++){
				ll rest=n-tw;
				if(rest<=0LL)break;
				for(int i=1;i<=40;i++){
					if(check(rest,i)){
						flag=true;
						goto gg;
					}
				}
				tw*=2LL;
			}
			gg:
			printf("%s\n",flag?"Yes":"No");
		}
	}
	return 0;
}
0