結果

問題 No.528 10^9と10^9+7と回文
コンテスト
ユーザー bal4u
提出日時 2019-06-28 10:14:30
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,673 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 197 ms
コンパイル使用メモリ 40,604 KB
最終ジャッジ日時 2026-02-22 03:42:21
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// 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