結果

問題 No.528 10^9と10^9+7と回文
ユーザー bal4ubal4u
提出日時 2019-06-28 10:14:30
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,673 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 31,208 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-01 01:00:28
合計ジャッジ時間 1,396 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 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,376 KB
testcase_06 AC 0 ms
4,376 KB
testcase_07 AC 0 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 0 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 0 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘ins’ 内:
main.c:9:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:18:21: 備考: in expansion of macro ‘gc’
   18 |         while ((c = gc()) > ' ') w++, *s++ = c & 0xf;
      |                     ^~
main.c: 関数 ‘out’ 内:
main.c:10:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   10 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:26:17: 備考: in expansion of macro ‘pc’
   26 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: No.528 10^9と10^9+7と回文
// 2019.6.28 bal4u

#include <stdio.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 ins(char *s) {
	int w = 0, c;
	while ((c = gc()) > ' ') w++, *s++ = c & 0xf;
	return w;
}

void out(int n) {
	int i;
	char ob[20];

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

#define M1 1000000000
#define M2 1000000007

char S[100005]; int w;

void check(int l, int r) {
	int i = l, f = 0;
	while (i < r) {
		if (S[i] > S[w-i-1]) f = 1;
		else if (S[i] < S[w-i-1]) {
			if (f) return;
			i = l-1;
			while (S[i] == 0) S[i--] = 9;
			S[i]--;
			return;
		}
		i++;
	}
}
	
int main()
{
	int i, k, x, a1, a2, t1, t2;
	ll p1, p2;
	
	w = ins(S);
	if (w == 1) { out(S[0]), out(S[0]); return 0; }
	k = w >> 1;
	check(w-k, w);
	if (--w & 1) k--;
	
//	printf("w=%d, k=%d, --> ", w+1, k);
//	for (i = 0; i <= w; i++) printf("%d", S[i]); printf("\n");

	if (S[0] == 0) a1 = a2 = 0;
	else {
		a1 = a2 = S[k] + (k > 0);
		i = k - 1, p1 = p2 = 10;
		while (i >= 0) {
			x = S[i] - (i == 0);
			a1 = (a1 + x * p1) % M1, a2 = (a2 + x * p2) % M2;
			p1 = p1 * 10 % M1, p2 = p2 * 10 % M2;
			i--;
		}
	}

	t1 = t2 = 9;
	for (i = 0; i < w; i++) {
		if (t1 == 0) break;
		a1 = (a1 + t1) % M1, a2 = (a2 + t2) % M2;
		if (++i >= w) break;
		a1 = (a1 + t1) % M1, a2 = (a2 + t2) % M2;
		t1 = t1 * 10LL % M1, t2 = t2 * 10LL % M2;
	}
	while (i < w) {
		a2 = (a2 + t2) % M2;
		if (++i >= w) break;
		a2 = (a2 + t2) % M2, t2 = t2 * 10LL % M2;
		i++;
	}
	out(a1), out(a2);
	return 0;
}
0