您的位置:首页 > 其它

HihoCoder 1033交错和(数位DP第三题)

2017-11-03 11:07 337 查看
(写挂了,有空再补)
时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1,定义交错和函数:

f(x) = a0 - a1 + a2 - ... + ( - 1)n - 1an - 1

例如:

f(3214567) = 3 - 2 + 1 - 4 + 5 - 6 + 7 = 4

给定

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<memory>
#include<cstring>
#include<cmath>
using namespace std;
#define LL long long
const LL Mod=1000000007;
LL a[20],cnt,L,R,S;
LL ans,dp[20][400][2][2][2];

LL dfs(LL pos,LL sum,LL limit,LL sign,LL fir,LL sub)
{
if(pos==0) return sum==S?sub:0;
if(!limit&&sign&&dp[pos][S-sum+200][1][1][1]) {
for(int i=1;i<=pos;i++) sub=sub*10%Mod;
sub=(sub+dp[pos][S-sum+200][1][1][1])%Mod;
return sub;
}
LL Up=limit?a[pos]:9;
LL tmp=0;
for(LL i=0;i<=Up;i++){
if(fir&&i==0) continue;
tmp=(tmp%Mod+dfs(pos-1,sum+(sign?1:-1)*i,limit&&i==Up,1-sign,0,sub*10%Mod+i))%Mod;
}
dp[pos][sum][limit][sign][fir]=tmp;
return tmp;
}

LL cal(int v)
{
cnt=0;ans=0;
while(v){
a[++cnt]=v%10;
v/=10;
}
for(LL i=1;i<=cnt;i++){
LL  tmp=dfs(i,200,i==cnt,1,1,0);//枚举第一位 ,避开前导0
ans+=tmp;
}
return ans;
}

int main()
{
scanf("%lld%lld%lld",&L,&R,&S);
S+=200;
printf("%lld",(cal(R)-cal(L-1)+Mod)%Mod);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: