結果

問題 No.577 Prime Powerful Numbers
ユーザー ryoissyryoissy
提出日時 2017-10-14 00:35:19
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,888 bytes
コンパイル時間 1,457 ms
コンパイル使用メモリ 159,588 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 21:58:43
合計ジャッジ時間 7,741 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
5,248 KB
testcase_01 AC 71 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 25 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:84:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   84 |         scanf("%d",&q);
      |         ~~~~~^~~~~~~~~
main.cpp:87:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   87 |                 scanf("%lld",&n);
      |                 ~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
#include <time.h>
#define INF 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 mod_pow(ll x,ll n,ll mod){
	ll res=1;
	while(n>0){
		if(n&1LL)res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}

ll calc(ll v,ll tgt,int po){
	ll ans=1;
	while(po>0){
		if(po&1LL)ans=ans*v;
		v=v*v;
		po>>=1;
	}
	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;
	for(int i=0;i<10;i++){
		ll v=rdn[i]%val;
		if(v==0LL)v=1;
		if(mod_pow(v,val-1LL,val)!=1LL)return false;
	}
	return true;
}

bool C(ll v,ll tgt,int po){
	ll res=1;
	while(po>0){
		if(po&1){
			if(log(tgt)+1<(log(res)+log(v)))return false;
			res*=v;
		}
		if(po>1 && log(tgt)+1<(log(v)+log(v)))return false;
		v*=v;
		po>>=1;
	}
	return res<=tgt;
}

bool check(ll val,int po){
	if(po==1)return is_prime(val);
	else{
		ll l=2,r=min(val,INF);
		if(po==3)r=min(val,1000000LL);
		if(po==4)r=min(val,100000LL);
		if(po==5)r=min(val,10000LL);
		if(po==6)r=min(val,1000LL);
		if(po>6) r=min(val,1000LL);
		if(!C(l,val,po))return false;
		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<10;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<=59;i++){
					if(check(rest,i)){
						flag=true;
						goto gg;
					}
				}
				tw*=2LL;
			}
			gg:
			printf("%s\n",flag?"Yes":"No");
		}
	}
	return 0;
}
0