題目鏈接:點擊查看
題目大意:
給了你一個極端大的數據集合的信息
N, SUM, X 如下
-
這個數據集合裏面的N位, 每一數位求和之後剛好等於SUM (比如四位數 1234 數位求和之後是 10);
-
它們都有N位, 十進制的(每一位都在0~9), 我們這裏降低點難度, 特別容許前導0的存在. 1234, 0123 都是合理的數;
-
這N位長度的數字字符串, 任意連續的三位數字構成的數據都能被X整除.
PS: 有可能 有空數據集
爲了減低難度你只要求出原來數據集合內有多少數據 mod 1000009 就好
題目分析:對數位dp不敏感,比賽的時候讀完這個題一點思路都沒有,賽後看了一眼別人代碼就恍然大悟,這個題目與其說是數位dp,不如說是記憶化搜索,因爲題目允許存在前導零,也就不用 limit 變量進行約束,從而實現就簡單了好多好多
套上模板就好了,有個點需要注意的是,dp數組需要開 int ,如果開 long long 的話會爆內存
代碼:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e6+100;
const int mod=1000009;
int dp[55][4505][15][15];//dp[pos][sum][pre1][pre2]
int b[55],s,x,n;
LL dfs(int pos,int sum,int pre1,int pre2)
{
if(pos==-1)
return sum==s;
if(dp[pos][sum][pre1][pre2]!=-1)
return dp[pos][sum][pre1][pre2];
int ans=0;
for(int i=0;i<=9;i++)
{
if(pre1!=-1&&pre2!=-1&&(pre1*100+pre2*10+i)%x)
continue;
ans=(ans+dfs(pos-1,sum+i,pre2,i))%mod;
}
dp[pos][sum][pre1][pre2]=ans;
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);
memset(dp,-1,sizeof(dp));
scanf("%d%d%d",&n,&s,&x);
cout<<dfs(n-1,0,-1,-1)<<endl;
return 0;
}