*【CodeForces - 214D 】Numbers (dp,组合数学)
題干:
Furik loves writing all sorts of problems, especially such that he can't solve himself. You've got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it.
There is integer?n?and array?a, consisting of ten integers, indexed by numbers from 0 to 9. Your task is to count the number of positive integers with the following properties:
- the number's length does not exceed?n;
- the number doesn't have leading zeroes;
- digit?i?(0?≤?i?≤?9)?occurs in the number at least?a[i]?times.
Input
The first line contains integer?n?(1?≤?n?≤?100). The next line contains 10 integers?a[0],?a[1],?...,?a[9]?(0?≤?a[i]?≤?100)?— elements of array?a. The numbers are separated by spaces.
Output
On a single line print the remainder of dividing the answer to the problem by?1000000007?(109?+?7).
Examples
Input
1 0 0 0 0 0 0 0 0 0 1Output
1Input
2 1 1 0 0 0 0 0 0 0 0Output
1Input
3 1 1 0 0 0 0 0 0 0 0Output
36Note
In the first sample number 9 meets the requirements.
In the second sample number 10 meets the requirements.
In the third sample numbers?10, 110, 210, 120, 103?meet the requirements. There are other suitable numbers, 36 in total.
解題報告:
? ? dp[i][j]表示:長度為i,用1~j數字所能構成的解的方案數。0的那一位單獨處理就可以了、、
AC代碼:(62ms)
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 4e5 + 5; const ll mod = 1000000000+7;int n; int a[15]; ll dp[150][22]; ll C[205][205]; void init() {C[0][0]=1;for(int i=1; i<=100; i++) {C[i][0]=1;for(int j=1; j<=i; j++) {C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;}} } int main() {init();cin>>n;for(int i = 0; i<=9; i++) scanf("%d",a+i);for(int i = a[1]; i<=n; i++) dp[i][1] = 1;//長度為i只包含1的 、for(int i = 1; i<=9; i++) {for(int j = 0; j<=n; j++) {for(int k = a[i]; k<=j; k++) {dp[j][i] = (dp[j][i] + dp[j-k][i-1]*C[j][k])%mod;}}}for(int i = 0; i<=n; i++) {for(int k = a[0]; k<i; k++) {dp[i][0] = (dp[i][0]+dp[i-k][9]*C[i-1][k])%mod;}}ll ans = 0;for(int i = 0; i<=n; i++) ans = (ans + dp[i][0]) % mod;printf("%lld\n",ans);return 0 ;}關于組合數的求法,還有一種92ms的,,打表到1e5才92ms、、、可以一試啊反正是On的、、
const ll mod = 1000000000+7; const ll N = 300000+5; const ll M = 3e5+3; int n; ll fac[1000005]; //階乘 ll inv_of_fac[1000005]; //階乘的逆元 int a[15]; ll dp[150][12]; ll qpow(ll x,ll n) {ll ret=1;for(; n; n>>=1){if(n&1) ret=ret*x%mod;x=x*x%mod;}return ret; } void init() {fac[1]=1;for(int i=2; i<=M; i++)fac[i]=fac[i-1]*i%mod;inv_of_fac[M]=qpow(fac[M],mod-2);for(int i=M-1; i>=0; i--)inv_of_fac[i]=inv_of_fac[i+1]*(i+1)%mod;//inv_of_fac[i]=qpow(fac[i],mod-2);//為什么不行啊 //也行 } ll C(ll a,ll b) {if(b>a) return 0;if(b==0) return 1;return fac[a]*inv_of_fac[b]%mod*inv_of_fac[a-b]%mod; }附:
三套代碼的dp思路以后看
#include <bits/stdc++.h> using namespace std; #define N 202 #define mod 1000000007typedef long long LL;int n, a[N], s[N]; LL c[N][N], f[N][N], ans;void prepare() {c[0][0] = 1;for (int i = 1; i <= 200; i ++) {c[i][0] = 1;for (int j = 1; j <= i; j ++) c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod;} } inline LL h(int n, int k) {return c[n+k-1][k]; } int main() {prepare();scanf("%d", &n);for (int i = 0; i <= 9; i ++) scanf("%d", &a[i]);for (int i = 1; i <= 9; i ++) s[i] = s[i-1] + a[i];int sum = s[9];f[0][0] = 1;for (int i = 1; i <= 9; i ++) {for (int j = s[i-1]; j <= n; j ++) {for (int k = a[i]; k <= n - j; k ++) {f[i][j+k] = (f[i][j+k] + f[i-1][j] * c[j+k][j]) % mod;}}}for (int zero = a[0]; zero <= n; zero ++) {for (int other = sum; other <= n - zero; other ++) {ans = (ans + f[9][other] % mod * h(other, zero)) % mod;}}printf("%I64d\n", ans);return 0; }?
代碼2
#include<cstdio> #include<cstring> int f[15][105]; int d[15]; int n; int mod=1000000007; int c[105][105]; int main() {c[0][0]=1;for(int i=1; i<=100; i++) {c[i][0]=1;for(int j=1; j<=i; j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;}scanf("%d",&n);int sum=0;int ans=0;for(int i=0; i<10; i++) {scanf("%d",d+i);sum+=d[i];}for(int i=1; i<=n; i++) {memset(f,0,sizeof(f));f[0][i]=1;for(int j=0; j<10; j++) {for(int k=0; k<=i; k++)if (f[j][k]) {for(int l=d[j]; l<=k; l++)f[j+1][k-l]=(f[j+1][k-l]+1ll*f[j][k]*c[k][l]%mod)%mod;}}ans=(ans+f[10][0])%mod;}n--;if (d[0]) {d[0]--;}for(int i=0; i<=n; i++) {memset(f,0,sizeof(f));f[0][i]=1;for(int j=0; j<10; j++) {for(int k=0; k<=i; k++)if (f[j][k]) {for(int l=d[j]; l<=k; l++)f[j+1][k-l]=(f[j+1][k-l]+1ll*f[j][k]*c[k][l]%mod)%mod;}}//printf("%d\n",f[10][0]);ans=(ans-f[10][0])%mod;}ans=(ans+mod)%mod;printf("%d\n",ans); }?
代碼3:
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<iostream> using namespace std;int a[10]; long long f[10][200]; long long C[300][300]; int main() {long long mod=1000000007;for (int i=1; i<=200; i++) {C[i][i]=C[i][0]=1;}C[0][0]=1;for (int i=2; i<=200; i++)for (int j=1; j<i; j++) {C[i][j]=C[i-1][j-1]+C[i-1][j];C[i][j]%=mod;}int n;cin>>n;int tot=0;for (int i=0; i<10; i++) {cin>>a[i];tot+=a[i];}memset(f,0,sizeof(f));for(int i=a[9]; i<=n; i++) {f[9][i]=1;}for (int i=8; i>0; i--) {if (a[i]==0) f[i][0]=f[i+1][0];for (int j=1; j<=n; j++)for (int k=a[i]; k<=j; k++) {f[i][j]+=f[i+1][j-k]*C[j][k];f[i][j]%=mod;}}for (int j=2; j<=n; j++)for (int k=a[0]; k<j; k++) {f[0][j]+=f[1][j-k]*C[j-1][k];f[0][j]%=mod;}// cout<<f[1][1]<<' '<<f[1][2]<<endl;long long ans=0;if (a[0]==0) {ans+=f[1][1];}for (int i=2; i<=n; i++) {ans+=f[0][i];ans%=mod;}cout<<ans%mod<<endl; }?
總結
以上是生活随笔為你收集整理的*【CodeForces - 214D 】Numbers (dp,组合数学)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Skype.exe - Skype是什么
- 下一篇: *【51nod - 1459】迷宫游戏(