51nod数字0-9的数量(数位dp)
2016-10-11 00:28
323 查看
给出一段区间a-b,统计这个区间内0-9出现的次数。 比如 10-19,1出现11次(10,11,12,13,14,15,16,17,18,19,其中11包括2个1),其余数字各出现1次。 Input 两个数a,b(1 <= a <= b <= 10^18) Output 输出共10行,分别是0-9出现的次数 Input示例 10 19 Output示例 1 11 1 1 1 1 1 1 1 1
好想说,你妈炸了。 这题我去,对拍了那么久,小数据完全没问题啊,大数据就错了。 尼玛最后是我初始化时for循环的时候i爆了,心态炸了,调了超级超级久。 果然自己辣鸡到不可想象。 初始化和上一道1的数量差不多。 这里我加了对0的处理。 00~99 000~999这样子,每个数字的个数都是一样的,最后我们只需要减掉我们补的多余的数字,个位多一个0,十位多10个0.... 然后我是分类讨论了一下。 唉,真的心态炸了,加上这两天感冒,事事不顺。
#include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cctype> #include<cmath> #include<ctime> #include<string> #include<stack> #include<deque> #include<queue> #include<list> #include<set> #include<map> #include<cstdio> #include<limits.h> #define MOD 1000000007 #define fir first #define sec second #define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin) #define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout) #define mes(x, m) memset(x, m, sizeof(x)) #define Pii pair<int, int> #define Pll pair<ll, ll> #define INF 1e9+7 #define Pi 4.0*atan(1.0) #define lowbit(x) (x&(-x)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long ll; typedef unsigned long long ull; const double eps = 1e-12; const int maxn = 35; using namespace std; inline int read(){ int x(0),op(1); char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')op=-1,ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*op; } ll l,r; ll dp[19][10]; int num[19]; void init() { mes(dp,0); for(int i=1;i<=18;++i){ for(int j=1;j<=9;++j){ if(1==j){ dp[i][j]=dp[i-1][9]*2+pow(10,i-1); } else{ dp[i][j]=dp[i][j-1]+dp[i-1][9]; } } dp[i][0]=(ll)i*pow(10,i-1); } } int cacl(ll n) { int k=0; while(n){ num[++k]=n%10; n/=10; } return k; } ll solve(int t,ll n) { mes(num,0); int k=cacl(n); ll res=0; for(int i=k;i>=1;--i){ //前缀要加上 if(t==num[i]){ res+=(n%(ll)pow(10,i-1)); } } for(int i=k;i>=1;--i){ if(t) //1~9 { if(0==num[i]){ continue; } if(1==num[i]){ res+=dp[i-1][9]; if(1==t){ res+=1; } } else{ if(num[i]>t){ res+=dp[i][num[i]-1]; } else{ res=res+dp[i][num[i]-1]-(ll)pow(10,i-1); //这里需要注意 } if(t==num[i]){ res+=1; } } } else{ //0 if(t==num[i]){ res+=1; } else{ res=res+dp[i][0]-(ll)(10-num[i])*dp[i-1][0]; } } } if(0==t){ //去除多余的0 ll m=1; while(n){ n/=10; res-=m; m*=10; } } return res; } int main() { //fin; //fout; init(); scanf("%lld%lld",&l,&r); for(int i=0;i<=9;++i){ cout<<(solve(i,r)-solve(i,l-1))<<endl; // cout<<solve(i,r)<<endl; } // cout<<endl; return 0; }
“`
相关文章推荐
- 51nod 1009 数字1的数量(数位dp)
- 51Nod 1042 数字0-9的数量 (数位DP
- 51nod 1042 数字0-9的数量(数位dp)
- 51nod 1042 数字0-9的数量 数位DP
- 数位dp 总结 51nod数字1的数量 数字0-9的数量
- 51Nod 1042 数字0-9的数量 (数位DP
- 51nod 1009 数字1的数量【数位dp】
- 51Nod 数字0到9的数量 (数位DP)
- 51nod 1042 数字0-9的数量【数位dp】
- 51nod 1042 数字0-9的数量 数位dp
- 【51nod 1009】数字1的数量 【数位DP 模板】
- 【51Nod】1042 - 数字0-9的数量(数位dp & 递归)
- 51nod:数字1的数量(线性dp or 数位dp)
- 51nod 1009 数字1的数量(数位dp)
- 51nod 1042 数字0-9的数量 数位DP
- 51nod 1042 数字0-9的数量(数位DP)
- 【数位dp入门】51nod 1009 数字1的数量
- 51Nod 1009 数字1的数量(数位dp)
- 51nod 1042 数字0-9的数量(数位dp)
- 51nod 1009 数字1的数量(数位dp)