結果

問題 No.752 mod数列
ユーザー bal4ubal4u
提出日時 2019-06-18 08:31:49
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 1,412 bytes
コンパイル時間 420 ms
コンパイル使用メモリ 31,292 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 06:58:40
合計ジャッジ時間 4,258 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 0 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 0 ms
4,380 KB
testcase_07 AC 0 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 0 ms
4,384 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 11 ms
4,380 KB
testcase_17 AC 21 ms
4,380 KB
testcase_18 AC 14 ms
4,380 KB
testcase_19 AC 16 ms
4,380 KB
testcase_20 AC 18 ms
4,384 KB
testcase_21 AC 18 ms
4,376 KB
testcase_22 AC 18 ms
4,380 KB
testcase_23 AC 19 ms
4,384 KB
testcase_24 AC 20 ms
4,380 KB
testcase_25 AC 7 ms
4,376 KB
testcase_26 AC 17 ms
4,380 KB
testcase_27 AC 12 ms
4,380 KB
testcase_28 AC 16 ms
4,384 KB
testcase_29 AC 18 ms
4,376 KB
testcase_30 AC 18 ms
4,376 KB
testcase_31 AC 0 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:10:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:18:24: 備考: in expansion of macro ‘gc’
   18 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘out’ 内:
main.c:11:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   11 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:28:17: 備考: in expansion of macro ‘pc’
   28 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: No.752 mod数列
// 2019.6.17 bal4u

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

typedef long long ll;

#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 out(ll n)  // 非負整数の表示(出力)
{
	int i;
	char b[50];

	if (!n) pc('0');
	else {
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

int sa[31650];
ll s[31650];
int P;

ll sum(int l, int r, int d) {
	if (l > r) return 0;
	return ((((P % r)<<1) + (ll)(r-l)*d)*(r-l+1)) >> 1;
}

int main()
{
	int i, r, n, pl, pr, Q, L, R, M;
	ll ans;

	P = in(), Q = in();
	r = (int)sqrt((double)P);
	M = P/r;
	for (i = 2; i <= M; i++) sa[i] = sa[i-1] + P % i;
	n = P; for (i = 1; i < r; i++) {
		int a = P % n, k = P/(i+1);
		s[i+1] = s[i] + ((((a<<1) + (ll)(n-k-1)*i)*(n-k)) >> 1);
		n = k;
	}
		
	while (Q--) {
		L = in(), R = in(), pl = P/L, pr = P/R;
		if (pr == 0) pr = 1;
		if (R <= M) ans = sa[R] - sa[L-1];
		else if (L <= M) {
			ans = sa[M] - sa[L-1] + (s[r]-s[pr]) - sum(R+1, P/pr, pr);
		}
		else if (L <= P) {
			ans = s[pl]-s[pr] + sum(L, P/pl, pl) - sum(R+1, P/pr, pr);
		}
		if (L > P) ans = (ll)P * (R-L+1);
		else if (R > P) ans += (ll)P * (R-P);
		out(ans);
	}
	return 0;
}
0