結果

問題 No.8030 ミラー・ラビン素数判定法のテスト
ユーザー fura
提出日時 2020-12-11 17:37:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 856 bytes
コンパイル時間 2,338 ms
コンパイル使用メモリ 194,640 KB
最終ジャッジ日時 2025-01-16 22:16:36
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 WA * 6
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:43:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   43 |         int q; scanf("%d",&q);
      |                ~~~~~^~~~~~~~~
main.cpp:45:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   45 |                 lint x; scanf("%lld",&x);
      |                         ~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

random_device seed_gen;
mt19937 rng(seed_gen());

lint modpow(lint a,lint k,int m){
	lint r=1%m;
	for(lint x=a%m;k>0;k>>=1,x=x*x%m) if(k&1) r=r*x%m;
	return r;
}

bool Miller_Rabin(lint n,int k=20){
	if(n<=1)   return false;
	if(~n&1)   return n==2;
	if(n%3==0) return n==3;

	int s=0; // n-1 = 2^s * d (d: odd)
	lint d=n-1;
	while(~d&1) s++, d>>=1;

	while(k--){
		lint a=rng()%(n-3)+2;
		lint x=modpow(a,d,n);
		if(x==1 || x==n-1) continue;
		bool b=false;
		for(int r=1;r<s;r++){
			x=__int128(x)*x%n;
			if(x== 1 ) return false;
			if(x==n-1){ b=true; break; }
		}
		if(!b) return false;
	}

	return true;
}

int main(){
	int q; scanf("%d",&q);
	rep(_,q){
		lint x; scanf("%lld",&x);
		printf("%lld %d\n",x,Miller_Rabin(x));
	}
	return 0;
}
0