51Nod 1043 幸运号码

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1043

题意:略。

思路:数位dp。设dp\left[i\right]\left[j\right]为第i位和为j的数量,所以容易推得

dp\left[i\right]\left[j\right]=\sum_{k=0}^{9}dp\left[i-1\right]\left[j-k\right]

,故

ans=\sum_{i=0}^{n*9}dp\left[n \right]\left[i \right]*\left(dp\left[n \right] \left[i \right]-dp\left[n-1 \right] \left[i \right]\right)\bmod MOD

, 后面是为了除去前导0。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=500000+5;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const double PI=acos(-1.0);
template<typename T> inline T gcd(T&a,T&b){return b==0?a:gcd(b,a%b);}
template<typename T> inline T lcm(T&a,T&b){return a/gcd(a,b)*b;}
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
int n;
ll dp[1005][9005],ans;
void init(){}
int main(){
    read(n);
    clr(dp,0);
    dp[0][0]=1;//输入1的时候,dp[1][0]没有可减去的前导0的数,所以把dp[0][0]变成 1
    for (int i=0;i<10;i++) dp[1][i]=1;
    for (int i=2;i<=n;i++){
        for (int j=0;j<=i*9;j++){
            for (int k=0;k<=j && k<=9;k++){
                dp[i][j]=(dp[i][j]+dp[i-1][j-k])%MOD;
            }
        }
    }
    for (int i=0;i<=n*9;i++){
        ans=(ans+dp[n][i]*(dp[n][i]-dp[n-1][i])%MOD)%MOD;
    }
    printf("%lld\n",ans);
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注