當前位置:
首頁 >
牛客 - 数位操作2(数位dp)
發布時間:2024/4/11
36
豆豆
生活随笔
收集整理的這篇文章主要介紹了
牛客 - 数位操作2(数位dp)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:
給了你一個極端大的數據集合的信息
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; }?
總結
以上是生活随笔為你收集整理的牛客 - 数位操作2(数位dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷 - P4168 [Violet]蒲
- 下一篇: 牛客 - 降维打击(dp)