結果

問題 No.1840 Random Painting
ユーザー 👑 ygussanyygussany
提出日時 2022-01-22 20:32:43
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 1,152 ms / 2,000 ms
コード長 823 bytes
コンパイル時間 224 ms
コンパイル使用メモリ 32,256 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-27 16:00:30
合計ジャッジ時間 13,358 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const long long Mod = 1000000000000000003, inv = 666666666666666669;

__int128 div_mod(__int128 x, __int128 y, __int128 z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

__int128 prob(int l, int k)
{
	return div_mod(l - k + 1, l + 1, Mod);
}

__int128 expect(int l, int k)
{
	return (__int128)k * (2 * l - k + 2) * inv % Mod;
}

int main()
{
	int N;
	char s[1000001];
	scanf("%d", &N);
	scanf("%s", s);
	
	int h, i, j, k, l;
	__int128 ans = 0;
	for (h = N - 1; s[h] != '0'; h--);
	for (i = 0, j = 1; j <= h; j++) {
		if (s[j] != '0') continue;
		
		l = j - (h - N) - 1;
		k = j - i;
		ans += (prob(l, k) * expect(l, k) + prob(l, l - k + 1) * expect(l, l - k + 1)) % Mod;
		
		i = j;
	}
	printf("%lld\n", ans % Mod);
	fflush(stdout);
	return 0;
}
0