记录编号 597342 评测结果 AAAAAAAAAA
题目名称 小b爱取模 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 2.110 s
提交时间 2024-11-26 20:09:35 内存使用 63.62 MiB
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,ll>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 1e7+10;

ll read(){
	ll x = 0,f = 1;char c = getchar();
	for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
	for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
	return x * f;
}

int n,k;
char c[N];
int b[N],s[N];

int main(){
	freopen("changgao_modulo.in","r",stdin);
	freopen("changgao_modulo.out","w",stdout);
	k = read();
	scanf("%s",c + 1);
	c[0] = '0',n = strlen(c + 1);
	for(int i = 1;i <= n;i++)b[i] = (k - c[i] + c[i-1]) % k;
	for(int i = 1;i <= n;i++)s[i] = s[i-1] + b[i];
	for(int i = k - 1;i >= 0;i--){
		int mi = 1e8;
		for(int j = n;j >= 1;j--){
			mi = min(mi,s[j]);
			if(mi >= k && b[j] == i)mi -= k,b[j] -= k;
		}
		for(int j = 1;j <= n;j++)s[j] = s[j-1] + b[j];
	}
	int ans = 0;
	for(int i = 1;i <= n;i++)ans += max(0,b[i]);
	printf("%d\n",ans);

	return 0;

}