結果

問題 No.3030 ミラー・ラビン素数判定法のテスト
ユーザー bal4ubal4u
提出日時 2019-05-18 22:06:35
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 226 ms / 9,973 ms
コード長 1,813 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 32,128 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 09:22:47
合計ジャッジ時間 1,343 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 123 ms
5,376 KB
testcase_05 AC 115 ms
5,376 KB
testcase_06 AC 48 ms
5,376 KB
testcase_07 AC 50 ms
5,376 KB
testcase_08 AC 48 ms
5,376 KB
testcase_09 AC 226 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:13:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   13 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:23:26: note: in expansion of macro 'gc'
   23 |                 if ((c = gc()) < '0') break;
      |                          ^~
main.c:14:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
   14 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:24:27: note: in expansion of macro 'pc'
   24 |                 if (echo) pc(c);
      |                           ^~

ソースコード

diff #

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

#include <stdio.h>
#include <stdlib.h>
#include <math.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

ull in(int *s3, int echo) {   // 非負整数の入力
	ull n = 0; int c, s = 0;
	while (1) {
		if ((c = gc()) < '0') break;
		if (echo) pc(c);
		n = 10 * n + (c & 0xf);
		s += c & 0xf;
	}
	*s3 = s;
	return n;
}

//// ラビン素数テスト
#if 1
#define mulmod128(a,b,n) ((__int128_t)a*b % n)
#else
#define mod(a,m) 	((a)%(m))
ull mulmod128(ull a, ull b, ull m) {
	ull ans = 0;
    a = mod(a, m), b = mod(b, m);
	while (b > 0) {
		if (b & 1) ans = mod(ans + a, m);
		a = mod(a << 1, m);
		b >>= 1;
	}
    return ans;
}
#endif

ull modpow(ull x, ull p, ull n) {
	ull r = 1;
	while (p) {
		if (p & 1) r = mulmod128(r, x, n);
		x = mulmod128(x, x, n);
		p >>= 1;
	}
	return r;
}

int ptbl[] = { 2, 325, 9375,28178,450775,9780504,1795265022 };
int miller_rabin(ull n) {
	int i, j, b, t;
	ull u, x;

	u = n-1, t = 0; while ((u & 1) == 0) u >>= 1, t++;
	for (j = 0; j < 7; j++) {
		if ((b = ptbl[j]) >= n) {
			b %= n;
			if (b == 0) continue;
		}
		x = modpow(b, u, n);
		if (x == 1 || x == n-1) continue;
		i = 1; while (1) {
			if (i++ == t) return 0;
			x = mulmod128(x, x, n);
			if (x == 1) return 0;
			if (x == n-1) break;
		}
	}
	return 1;
}

int main()
{
	int n, s3, ans;
	ull x;
	static int p[16] = { 0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0 };
	
	n = (int)in(&s3, 0);
	while (n--) {
		ans = 0;
		x = in(&s3, 1);
		if (!(x >> 4)) ans = p[x];
		else if ((x & 1) && (s3 % 3) && (x % 5)) ans = miller_rabin(x);
		pc(' '), pc('0' + ans), pc('\n');
	}
	return 0;
}
0