結果

問題 No.854 公平なりんご分配
ユーザー bal4ubal4u
提出日時 2019-07-27 12:32:39
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 91 ms / 3,153 ms
コード長 4,572 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 32,576 KB
実行使用メモリ 120,012 KB
最終ジャッジ日時 2023-09-15 06:04:43
合計ジャッジ時間 6,153 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
9,872 KB
testcase_13 AC 2 ms
9,892 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
7,796 KB
testcase_16 AC 2 ms
9,904 KB
testcase_17 AC 2 ms
9,864 KB
testcase_18 AC 2 ms
9,896 KB
testcase_19 AC 2 ms
9,840 KB
testcase_20 AC 2 ms
7,796 KB
testcase_21 AC 2 ms
9,980 KB
testcase_22 AC 18 ms
91,804 KB
testcase_23 AC 20 ms
106,240 KB
testcase_24 AC 22 ms
118,492 KB
testcase_25 AC 19 ms
102,056 KB
testcase_26 AC 20 ms
106,116 KB
testcase_27 AC 21 ms
112,236 KB
testcase_28 AC 14 ms
71,304 KB
testcase_29 AC 15 ms
77,460 KB
testcase_30 AC 19 ms
102,088 KB
testcase_31 AC 22 ms
112,352 KB
testcase_32 AC 28 ms
118,740 KB
testcase_33 AC 28 ms
118,616 KB
testcase_34 AC 34 ms
118,900 KB
testcase_35 AC 30 ms
118,900 KB
testcase_36 AC 26 ms
114,308 KB
testcase_37 AC 28 ms
118,892 KB
testcase_38 AC 26 ms
118,776 KB
testcase_39 AC 40 ms
118,980 KB
testcase_40 AC 29 ms
118,596 KB
testcase_41 AC 31 ms
118,784 KB
testcase_42 AC 33 ms
118,904 KB
testcase_43 AC 34 ms
118,848 KB
testcase_44 AC 36 ms
118,936 KB
testcase_45 AC 35 ms
118,576 KB
testcase_46 AC 40 ms
118,872 KB
testcase_47 AC 29 ms
118,780 KB
testcase_48 AC 32 ms
118,884 KB
testcase_49 AC 31 ms
119,004 KB
testcase_50 AC 28 ms
118,868 KB
testcase_51 AC 40 ms
118,912 KB
testcase_52 AC 33 ms
118,584 KB
testcase_53 AC 28 ms
118,592 KB
testcase_54 AC 32 ms
118,628 KB
testcase_55 AC 26 ms
118,696 KB
testcase_56 AC 25 ms
118,592 KB
testcase_57 AC 27 ms
118,704 KB
testcase_58 AC 32 ms
118,440 KB
testcase_59 AC 25 ms
118,596 KB
testcase_60 AC 29 ms
118,612 KB
testcase_61 AC 25 ms
118,436 KB
testcase_62 AC 30 ms
118,664 KB
testcase_63 AC 28 ms
118,548 KB
testcase_64 AC 24 ms
118,532 KB
testcase_65 AC 31 ms
118,804 KB
testcase_66 AC 29 ms
118,548 KB
testcase_67 AC 32 ms
118,732 KB
testcase_68 AC 30 ms
118,544 KB
testcase_69 AC 29 ms
118,884 KB
testcase_70 AC 25 ms
118,648 KB
testcase_71 AC 26 ms
118,708 KB
testcase_72 AC 29 ms
118,428 KB
testcase_73 AC 28 ms
118,752 KB
testcase_74 AC 33 ms
118,904 KB
testcase_75 AC 28 ms
118,624 KB
testcase_76 AC 30 ms
118,780 KB
testcase_77 AC 32 ms
118,840 KB
testcase_78 AC 33 ms
118,540 KB
testcase_79 AC 32 ms
118,664 KB
testcase_80 AC 32 ms
118,752 KB
testcase_81 AC 29 ms
118,708 KB
testcase_82 AC 51 ms
119,960 KB
testcase_83 AC 50 ms
119,888 KB
testcase_84 AC 51 ms
119,908 KB
testcase_85 AC 22 ms
4,376 KB
testcase_86 AC 6 ms
4,376 KB
testcase_87 AC 57 ms
120,012 KB
testcase_88 AC 57 ms
119,840 KB
testcase_89 AC 56 ms
119,952 KB
testcase_90 AC 59 ms
119,888 KB
testcase_91 AC 60 ms
120,012 KB
testcase_92 AC 91 ms
119,900 KB
testcase_93 AC 89 ms
119,936 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:9:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:17:24: 備考: in expansion of macro ‘gc’
   17 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘outs’ 内:
main.c:10:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:22:33: 備考: in expansion of macro ‘pc’
   22 | void outs(char *s) { while (*s) pc(*s++); pc('\n'); }
      |                                 ^~

ソースコード

diff #

// yukicoder: No.854 公平なりんご分配
// 2019.7.27 bal4u

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

#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); while ((c = gc()) >= '0');
	return n;
}

void outs(char *s) { while (*s) pc(*s++); pc('\n'); }

#define MAX 100005
#define SIZE  14
#define TBL 305
int id[2003];
int factor[50], power[50], sz;
int sq[2003] = { 0,1 };

int ptbl[TBL] = {0,
	2,	 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,1009,1013, 
 1019,1021,1031,1033,1039,1049,1051,1061,1063,1069, 
 1087,1091,1093,1097,1103,1109,1117,1123,1129,1151, 
 1153,1163,1171,1181,1187,1193,1201,1213,1217,1223, 
 1229,1231,1237,1249,1259,1277,1279,1283,1289,1291, 
 1297,1301,1303,1307,1319,1321,1327,1361,1367,1373, 
 1381,1399,1409,1423,1427,1429,1433,1439,1447,1451, 
 1453,1459,1471,1481,1483,1487,1489,1493,1499,1511, 
 1523,1531,1543,1549,1553,1559,1567,1571,1579,1583, 
 1597,1601,1607,1609,1613,1619,1621,1627,1637,1657, 
 1663,1667,1669,1693,1697,1699,1709,1721,1723,1733, 
 1741,1747,1753,1759,1777,1783,1787,1789,1801,1811, 
 1823,1831,1847,1861,1867,1871,1873,1877,1879,1889, 
 1901,1907,1913,1931,1933,1949,1951,1973,1979,1987, 
 1993,1997,1999 };

int prime_factor(int n) {
	int i, b, d, w;

	sz = 0, b = (int)sqrt((double)n);
	for (i = 1; n > 1; i++) {
		if ((d = ptbl[i]) == 0) break;
		if (d > b) break;
		if (n % d) continue;
		factor[sz] = d, w = 0;
		do n /= d, w++;
		while (n % d == 0);
		power[sz++] = w;
	}
	if (n >= 2003) return 0;
	if (n > 1) factor[sz] = n, power[sz++] = 1;
	return 1;
}

int bit[TBL][MAX], bitn;

void add(int k, int i, int v) {
    while (i <= bitn) bit[k][i] += v, i += i & -i;
}
 
int sum(int k, int i) {
    int s = 0;
    while (i > 0) s += bit[k][i], i -= i & -i;
    return s;
}

void calc_prime(int k, int n) {
	int i, v, w, lim;
	
	lim = sq[n];
	for (i = 1; n > 1 && (v = ptbl[i]) <= lim; i++) {
		if (n % v) continue;
		w = 0;
		do n /= v, w++;
		while (n % v == 0);
		add(i, k, w);
	}
	if (n > 1) add(id[n], k, 1);
}

int zero[MAX], zw;
// a[i]>=key という条件を満たす最小のi
int lower_bound(int x) {
  int m, l = 0, r = zw;
  while (l+1 < r) {
    m = (l+r)/2;
    if (zero[m] < x) l = m; else r = m;
  }
  return zero[l] < x? r: l;
}

// a[i]>key という条件を満たす最小のi
int upper_bound(int l, int x) {
  int m, r = zw;
  while (l+1 < r) {
    m = (l+r)/2;
    if (zero[m] <= x) l = m; else r = m;
  }
  return zero[l] <= x? r: l;
}

#define SQ2000	44   // sqrt(2000)
int main()
{
	int i, k, Q, ans, _P, _L, _R;

	// 初期化処理
	k = 1; for (i = 1; i <= SQ2000; i++) while (k < i*i) sq[k++] = i-1;
	i--; while (k <= 2000) sq[k++] = i;
	for (i = 1; i < TBL; i++) id[ptbl[i]] = i;
	
	// 入力を素因数分解して、BIT木にしまう
	bitn = in(); for (i = 1; i <= bitn; i++) {
		int a = in(); if (a) calc_prime(i, a);
		else zero[zw++] = i;
	}
	
	Q = in(); _P = -1;
	while (Q--) {
		int P = in(), L = in(), R = in();
		if (P == _P && L == _L && R == _R) goto done;
		_P = P, _L = L, _R = R;
		
		ans = 1;
		if (P == 1) goto done;   // ひとり(P=1)だけに分けるケース
		if (zw) {				 // zero (0) が [L,R] に存在するケース
			k = lower_bound(L);
			if (upper_bound(k, R) >= k+1) goto done;
		}
		if (prime_factor(P)) {   // 一般のケース
			for (i = 0; i < sz; i++) {
				k = id[factor[i]];
				int s = sum(k, R);
				if (L > 1) s -= sum(k, L-1);
				if (s < power[i]) { ans = 0; break; }  // 素因数の数の比較
			}
		} else ans = 0;    // Pに2000を超えた素因数が含まれたケース
done:	outs(ans? "Yes": "NO");
	}
	return 0;
}
0