結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー bal4ubal4u
提出日時 2019-05-12 17:30:26
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 1,795 bytes
コンパイル時間 360 ms
コンパイル使用メモリ 30,848 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 13:12:10
合計ジャッジ時間 1,210 ms
ジャッジサーバーID
(参考情報)
judge1 / 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 2 ms
5,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 97 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:12:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   12 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:20:24: note: in expansion of macro 'gc'
   20 |         int n = 0, c = gc();
      |                        ^~
main.c: In function 'outs':
main.c:13:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
   13 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:37:33: note: in expansion of macro 'pc'
   37 | void outs(char *s) { while (*s) pc(*s++); }  // 文字列の表示
      |                                 ^~

ソースコード

diff #

// yukicoder 3030 ミラー・ラビン素数判定法のテスト
// 2019.5.12 bal4u

#include <stdio.h>
#include <stdlib.h>

typedef long long ll;
typedef unsigned long long ull;

//// 高速入出力
#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void ins(char *s) {  // 文字列の入力 スペース以下の文字で入力終了
	do *s = gc();
	while (*s++ > ' ');
	*(s-1) = 0;
}

long long s2ll(char *s) {   // 非負整数への変換
	long long n = 0;
	while (*s) n = 10*n + (*s++ & 0xf);
	return n;
}

void outs(char *s) { while (*s) pc(*s++); }  // 文字列の表示

//// ラビン素数テスト
ull powmod(ull a, ull k, ull n)
{
	ull p, bit;

	p = 1; for (bit = 0x8000000000000000ULL; bit > 0; bit >>= 1) {
		if (p > 1) p = (__int128_t)p*p % n;
		if (k & bit) p = (__int128_t)p*a % n;
	}
	return p % n;
}

int suspect(int b, ull n)
{
	int i;
	ull t, u, x0, x1;

	u = n - 1, t = 0; while ((u & 1) == 0) u >>= 1, t++;
	x0 = powmod(b, u, n);
	for (i = 1; i <= t; i++) {
		x1 = (__int128_t)x0*x0 % n;
		if (x1 == 1 && x0 != 1 && x0 != n-1) return 0;
		x0 = x1;
	}
	return x1 == 1;
}

int miller_rabin(ull n) {
	ull a, r, k, m;
	int p, t;
	
	if (n <= 1) return 0;
	if (n == 2 || n == 3) return 1;
	if ((n & 1) == 0) return 0;
	if (!suspect(2, n)) return 0;
	if (n <= 1000000) {
		if (!suspect(3, n)) return 0;
	} else {
		if (!suspect(7, n)) return 0;
		if (!suspect(61, n)) return 0;
	}
	return 1;
}

char s[25];

int main()
{
	int n;
	long long x;
	
	n = in(); while (n--) {
		ins(s), outs(s), pc(' ');
		pc('0' + miller_rabin(s2ll(s))), pc('\n');
	}
	return 0;
}
0