結果

問題 No.368 LCM of K-products
ユーザー bal4ubal4u
提出日時 2019-04-15 08:52:59
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 3,829 bytes
コンパイル時間 909 ms
コンパイル使用メモリ 34,888 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-23 21:29:11
合計ジャッジ時間 2,067 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
4,348 KB
testcase_01 AC 12 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 5 ms
4,348 KB
testcase_04 AC 3 ms
4,348 KB
testcase_05 AC 42 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 5 ms
4,348 KB
testcase_14 AC 7 ms
4,348 KB
testcase_15 AC 7 ms
4,348 KB
testcase_16 AC 7 ms
4,348 KB
testcase_17 AC 7 ms
4,348 KB
testcase_18 AC 8 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 6 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 7 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 4 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:11:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   11 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:24: note: in expansion of macro 'gc'
   17 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: No.368 LCM of K-products
// 2019.4.15 bal4u

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

//// 高速入力
#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif
int in()    // 非負整数の入力
{
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

//// 本問題関連グローバルデータ
#define MOD 1000000007
int hi[10000], *tbl[10000], fa[10000];
int sz;

//// ハッシュテーブル(値からIDを得る)
#define HASHSIZ 99991
typedef struct { int n, id; } HASH;
HASH hash[HASHSIZ+5], *hashend = hash + HASHSIZ;

int lookup(int n)
{
	HASH *p = hash + n % HASHSIZ;
	while (p->n) {
		if (p->n == n) return p->id;
		if (++p == hashend) p = hash;
	}
	return -1;
}

int insert(int n, int id)
{
	HASH *p = hash + n % HASHSIZ;
	while (p->n) {
		if (p->n == n) return p->id;
		if (++p == hashend) p = hash;
	}
	p->n = n, p->id = id;
	return -1;
}

//// 素因数分解モジュール
#define MAX 1005
#define SIZE  25  // 先頭の10個素因数の積 2x3x5..x29 = 6.4x10^9
int factor[MAX][SIZE], power[MAX][SIZE], len[MAX];

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 };

void prime_factor(int id, int n)
{
	int i, k, d, size;
	int *pp;

	size = 0;
	if ((n & 1) == 0) {
		factor[id][size] = 2;
		if ((k = insert(2, sz)) < 0) { k = sz++, fa[k] = 2; }
		hi[k]++;
		do n >>= 1, power[id][size]++;
		while ((n & 1) == 0);
		size++;
	}
	for (pp = ptbl; n > 1 && *pp > 0; pp++) {
		if (n % *pp) continue;
		d = *pp;
		factor[id][size] = d;
		if ((k = insert(d, sz)) < 0) { k = sz++, fa[k] = d; }
		hi[k]++;
		do n /= d, power[id][size]++;
		while (n % d == 0);
		size++;
	}
	if (n > 1) {
		int b = (int)sqrt(n);
		for (i = 1009; n > 1; i += 2) {
			if (i > b) {
				factor[id][size] = n, power[id][size++] = 1;
				if ((k = insert(n, sz)) < 0) { k = sz++; fa[k] = n;	}
				hi[k]++;
				break;
			}
			if (n % i == 0) {
				factor[id][size] = i;
				if ((k = insert(i, sz)) < 0) { k = sz++; fa[k] = i;	}
				hi[k]++;
				do n /= i, power[id][size]++;
				while (n % i == 0);
				size++;
			}
		}
	}
	len[id] = size;
}

//// powの高速計算
int bigPow(int x, int p)
{
	int r = 1;
	while (p) {
		if (p & 1) r = (long long)r * x % MOD;
		x = (long long)x * x % MOD;
		p >>= 1;
	}
	return r;
}

//// 本問題関連
int cmp(const void *a, const void *b) { return *(int *)b - *(int *)a; }
int main()
{
	int i, j, k, p, N, K;
	long long ans;

	N = in(), K = in();
	for (i = 0; i < N; i++) prime_factor(i, in());
	for (i = 0; i < sz; i++) tbl[i] = malloc(hi[i] * sizeof(int));
	memset(hi, 0, sizeof(int)*sz);
	for (i = 0; i < N; i++) {
		for (j = 0; j < len[i]; j++) {
			k = lookup(factor[i][j]);
			tbl[k][hi[k]++] = power[i][j];
		}
	}
	ans = 1;
	for (i = 0; i < sz; i++) {
		if (hi[i] > K) qsort(tbl[i], hi[i], sizeof(int), cmp);
		p = 0; for (j = 0; j < K && j < hi[i]; j++) p += tbl[i][j];
		ans = (ans * bigPow(fa[i], p)) % MOD;
	}
	printf("%d\n", (int)ans);
	return 0;
}
0