Codeforces Round #157 (Div. 1) B. Little Elephant and Elections
B. Little Elephant and Electionstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
There have recently been elections in the zoo. Overall there were 7 main political parties: one of them is the Little Elephant Political Party, 6 other parties have less catchy names.
Political parties find their number in the ballot highly important. Overall there are m possible numbers: 1,?2,?...,?m. Each of these 7parties is going to be assigned in some way to exactly one number, at that, two distinct parties cannot receive the same number.
The Little Elephant Political Party members believe in the lucky digits 4 and 7. They want to evaluate their chances in the elections. For that, they need to find out, how many correct assignments are there, such that the number of lucky digits in the Little Elephant Political Party ballot number is strictly larger than the total number of lucky digits in the ballot numbers of 6 other parties.
Help the Little Elephant Political Party, calculate this number. As the answer can be rather large, print the remainder from dividing it by1000000007 (109?+?7).
InputA single line contains a single positive integer m (7?≤?m?≤?109) — the number of possible numbers in the ballot.
OutputIn a single line print a single integer — the answer to the problem modulo 1000000007 (109?+?7).
Sample test(s)input7output
0input
8output
1440
数位dp,可以,先把所有有i个4,7的个数的数的个数用数位dp,求出来,然后,就是这10之间的组合数了!用一个简单搜索就可以了!
#include <stdio.h>#include <iostream>#include <string.h>#include <stdio.h>#define MOD 1000000007using namespace std;__int64 c[11][11],dp[11],ans;int pri[30];void dfs(int pos,int x,__int64 s){ if(pos>5){ans=(ans+s)%MOD;return ;} for(int i=0;i<x;i++) if(dp[i]) dp[i]--,dfs(pos+1,x-i,(dp[i]+1)*s%MOD),dp[i]++;}int main (){ int m,i,j,k; for(i=0;i<11;i++) for(j=0;j<=i;j++) c[i][j]=i&&j?c[i-1][j-1]+c[i-1][j]:1; while(scanf("%d",&m)!=EOF){ int n=0,cnt=0; __int64 len;m++; memset(dp,0,sizeof(dp));dp[0]--; while(m){pri[++n]=m%10;m/=10;} for(i=n;i>0;i--){ for(j=0;j<pri[i];j++){ len=(__int64)1<<(i-1); for(k=i-1;k>=0;k--){ dp[cnt+k+(j==4||j==7)]=(dp[cnt+k+(j==4||j==7)]+c[i-1][k]*len)%MOD; len<<=2; } } cnt+=pri[i]==4||pri[i]==7; } for(ans=0,i=0;i<10;i++){ if(dp[i])dp[i]--,dfs(0,i,dp[i]+1),dp[i]++; } cout<<ans<<endl; } return 0;}