結果

問題 No.577 Prime Powerful Numbers
ユーザー ryoissyryoissy
提出日時 2017-10-13 23:27:04
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,408 bytes
コンパイル時間 1,266 ms
コンパイル使用メモリ 159,412 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-28 21:50:29
合計ジャッジ時間 2,803 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 346 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:63:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   63 |         scanf("%d",&q);
      |         ~~~~~^~~~~~~~~
main.cpp:66:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   66 |                 scanf("%lld",&n);
      |                 ~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
#include <time.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

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

ll calc(ll v,ll tgt,int po){
	ll ans=1;
	for(int i=0;i<po;i++){
		ans*=v;
	}
	return (ans==tgt);
}

bool is_prime(ll val){
	if(val==1LL)return false;
	for(int i=0;i<10000;i++){
		ll v=rand()%val;
		if(v==0)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;
	for(int i=0;i<po;i++){
		if((tgt+v)/v<res)return false;
		res*=v;
	}
	return res<=tgt;
}

bool check(ll val,int po){
	if(po==1)return is_prime(val);
	else{
		ll l=1,r=(ll)pow(val,(double)1.0/po)+1LL;
		while(l+1<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){
	srand((unsigned)time(NULL));
	int q;
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		ll n;
		scanf("%lld",&n);
		if(n==2){
			printf("No\n");
		}else if(n%2LL==0){
			printf("Yes\n");
		}else{
			ll tw=2;
			bool flag=false;
			while(n-tw>0){
				ll rest=n-tw;
				for(int i=1;i<=60;i++){
					if(check(rest,i))flag=true;
				}
				if(flag)break;
				if(n-tw<=tw)break;
				tw*=2LL;
			}
			printf("%s\n",flag?"Yes":"No");
		}
	}
	return 0;
}
0