結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー akakimidoriakakimidori
提出日時 2018-02-09 10:45:19
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 411 ms / 9,973 ms
コード長 895 bytes
コンパイル時間 540 ms
コンパイル使用メモリ 22,400 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:15:11
合計ジャッジ時間 2,409 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 218 ms
5,376 KB
testcase_05 AC 204 ms
5,376 KB
testcase_06 AC 55 ms
5,376 KB
testcase_07 AC 54 ms
5,376 KB
testcase_08 AC 55 ms
5,376 KB
testcase_09 AC 411 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘run’:
main.c:45:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   45 |   scanf("%d",&n);
      |   ^~~~~~~~~~~~~~
main.c:48:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   48 |     scanf("%lld",&x);
      |     ^~~~~~~~~~~~~~~~

ソースコード

diff #

#include<stdio.h>

typedef long long int int64;

int64 modPow(int64 r,int64 n,int64 mod){
  int64 res=1;
  int64 t=r;
  while(n>0){
    if(n&0x01) res=(int64)((__int128)res*t%mod);
    t=(int64)((__int128)t*t%mod);
    n>>=1;
  }
  return res;
}

int isPrime(const int64 n){
  if(n<=1) return 0;
  if(n<=3) return 1;
  if(n%2==0) return 0;
  int64 d=n-1;
  int r=0;
  while(d%2==0){
    d/=2;
    r++;
  }

  const int aa[]={2,3,5,7,11,13,17,19,23,29,31,37};
  int i;
  for(i=0;i<12 && aa[i]<=n-2;i++){
    int64 a=aa[i];
    int64 x=modPow(a,d,n);
    if(x==1) continue;
    int k;
    for(k=0;k<r;k++){
      if(x==n-1) break;
      x=(int64)((__int128)x*x%n);
    }
    if(k==r) return 0;
  }
  return 1;
}

void run(void){
  int n;
  scanf("%d",&n);
  while(n>0){
    int64 x;
    scanf("%lld",&x);
    printf("%lld %d\n",x,isPrime(x));
    n--;
  }
}

int main(void){
  run();
  return 0;
}
0