結果
| 問題 |
No.577 Prime Powerful Numbers
|
| コンテスト | |
| ユーザー |
bal4u
|
| 提出日時 | 2019-05-16 19:56:12 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 43 ms / 2,000 ms |
| コード長 | 3,528 bytes |
| コンパイル時間 | 1,236 ms |
| コンパイル使用メモリ | 31,488 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-17 05:39:34 |
| 合計ジャッジ時間 | 1,319 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 10 |
コンパイルメッセージ
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:21:27: note: in expansion of macro 'gc'
21 | ll n = 0; int c = gc();
| ^~
main.c: In function 'outs':
main.c:14:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
14 | #define pc(c) putchar_unlocked(c)
| ^~~~~~~~~~~~~~~~
main.c:27:33: note: in expansion of macro 'pc'
27 | void outs(char *s) { while (*s) pc(*s++); }
| ^~
ソースコード
// yukicoder: No.577 Prime Powerful Numbers
// 2019.5.16 bal4u
#include <stdio.h>
#include <ctype.h>
#include <string.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
ll in()
{
ll n = 0; int c = gc();
while (isspace(c)) c = gc();
do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
return n;
}
void outs(char *s) { while (*s) pc(*s++); }
// 数値のハッシュ関数。素数のべき乗を登録しておく
#define HASHSIZ 19997
ll hash[HASHSIZ+5], *hashend = hash + HASHSIZ;
int lookup(ll n)
{
ll *p = hash + (int)(n % HASHSIZ);
while (*p) {
if (*p == n) return 1;
if (++p == hashend) p = hash;
}
return 0;
}
void insert(ll n)
{
ll *p = hash + (int)(n % HASHSIZ);
while (*p) {
if (*p == n) return;
if (++p == hashend) p = hash;
}
*p = n;
}
#define INF 1000000000000000000LL // 10^18
int ptbl[] = {
3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
661, 673, 677, 683, 691, 701, 709, 719, 727, 733,
739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863,
877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997, 0 };
int yes[11] = {0,0,0,0,1,1,1,1,1,1,1};
char *msg[] = { "No\n", "Yes\n" };
void init()
{
int i, b;
ll a;
for (i = 0; b = ptbl[i]; i++) {
a = b; while (a > 0 && a < INF) insert(a), a *= b;
}
}
//// ラビン素数テスト
#define mulmod128(a,b,n) ((__int128_t)a*b % n)
ull powmod(ull a, ull k, ull n)
{
ull bit = 0x2000000000000000LL, p = 1;
while (bit) {
if (p > 1) p = mulmod128(p, p, n);
if (k & bit) p = mulmod128(p, a, n);
bit >>= 1;
}
return p;
}
unsigned xorshift(int id) {
static unsigned y = 2463534242;
y = y ^ (y << 13), y = y ^ (y >> 17), y = y ^ (y << 5);
return y;
}
ull gcd(ull a, ull b) {
ull r;
while (b != 0) r = a % b, a = b, b = r;
return a;
}
int suspect(ull n) {
int i, j, b;
ull t, u, x;
u = n-1, t = 0; while ((u & 1) == 0) u >>= 1, t++;
for (j = 0; j < 6; j++) {
do b = xorshift(j) % n;
while (gcd(b, n) > 1);
x = powmod(b, u, n);
if (x == 1 || x == n-1) continue;
for (i = 1; i < t; i++) {
x = mulmod128(x, x, n);
if (x == 1) return 0;
if (x == n-1) break;
}
if (i == t) return 0;
}
return 1;
}
int miller_rabin(ull n) {
if (n <= 1) return 0;
if (n == 2 || n == 3) return 1;
if ((n & 1) == 0) return 0;
return suspect(n);
}
int main()
{
int i, Q;
ll N, a;
init();
Q = (int)in(); while (Q--) {
N = in();
if (N <= 10) outs(msg[yes[N]]);
else if ((N & 1) == 0) outs(msg[1]);
else {
for (i = 1; i <= 59; i++) {
a = N - (1LL << i);
if (a <= 0) { i = 60; break; }
if (lookup(a)) break;
if (miller_rabin(a)) break;
}
outs(msg[i <= 59]);
}
}
return 0;
}
bal4u