[题解] Exact Payment
Glassy_Sky · · 题解
题意
有
Solution
取若干相加
但是
由于是针对每个数位上的问题,容易联想到一位位地做背包,最多只用做
现在只需要考虑每一位怎么求最大值。
能影响当前位的,显然只有当前位及更低位,与更高位没关系。可以先取个模把高位去掉。
发现在背包中,有很多都没有实际贡献。
考虑对
当
即两数最高位相距小于等于
为什么?
考虑
都是加上一个
即
差距仍然是小于等于
那么,
试想一个数轴,取
所以
状态最多的情况就是每隔
具体的看代码。
code
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e4+5;
int n,tot;
long long a[maxn];
long long num[maxn],ans=0;
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
long long p=1;
for(int i=1;i<=17;i++) { //枚举每一位
p=p*10; //模数,把无关位去掉
tot=1;
num[1]=0;
for(int j=1;j<=n;j++) {
for(int k=1;k<=tot;k++)
num[tot+k]=(num[k]+a[j])%p; //背包 + 取模
sort(num+1,num+1+2*tot);
long long buf=2,cha=p/10;
for(int k=3;k<=2*tot;k++) //把无用的状态去掉
if(num[k]-num[buf-1]<=cha) num[buf]=num[k];
else num[++buf]=num[k];
tot=buf;
}
ans+=num[tot]/(p/10); //贡献最高位最大值
}
printf("%lld",ans);
return 0;
}