結果
| 問題 |
No.8030 ミラー・ラビン素数判定法のテスト
|
| ユーザー |
|
| 提出日時 | 2017-10-22 16:44:45 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,261 bytes |
| コンパイル時間 | 341 ms |
| コンパイル使用メモリ | 32,640 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-18 15:52:35 |
| 合計ジャッジ時間 | 1,181 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 WA * 6 |
ソースコード
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
typedef unsigned long long ull;
long long mul(long long x,long long y,long long m){
/*
ull z=0;
for(;y;y>>=1){
if(y&1){z+=x;if(z>=m)z-=m;}
x+=x;if(x>=m)x-=m;
}
return z;
*/
long long q=((double)x*(double)y/(double)m);
long long r=x*y-m*q;
r=(r%m+m)%m;
return r;
}
ull powmod(ull x,ull y,ull m){
ull z=1;
for(;y;y>>=1){
if(y&1)z=mul(z,x,m);
x=mul(x,x,m);
}
return z;
}
static unsigned int x = 123456789;
static unsigned int y = 362436069;
static unsigned int z = 521288629;
static unsigned int w = 88675123;
unsigned int xor_rand(){
unsigned int t;
t=x^(x<<11);
x=y;y=z;z=w;
return w=(w^(w>>19)) ^ (t^(t>>8));
}
ull gcd(ull x,ull y){return y?gcd(y,x%y):x;}
bool miller_rabin(ull n){
if(n==2)return true;
if(n==1||n%2==0)return false;
ull d=n-1,s=0,a=1;
for(;d%2==0;d/=2)s+=1;
for(int k=7;k--;){ //todo
//for(a++;gcd(a,n)!=1;a++); //todo
for(a=0;gcd(a,n)!=1;)a=xor_rand()%~-n+1;
ull r=powmod(a,d,n);
if(r==1||r==n-1)continue;
int t=s;
for(;t;t--){
r=powmod(r,2,n);
if(r==n-1)break;
}
if(!t)return false;
}
return true;
}
int main(){
ull t,n;
for(scanf("%llu",&t);t--;printf("%llu %d\n",n,miller_rabin(n)))scanf("%llu",&n);
}