• 题解 POJ2282 【The Counting Problem】


    题目链接:Link

    Problem

    Solution

    这题其实就是一道数位dp的板子题。。。但是我一开始想复杂了。。。莫名其妙WA了。。。拍几万组数据都找不到锅在哪儿。。。心肌梗塞的感觉。。。
    坑点:a可能大于b

    Code

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    LL a,b,cnta[20],cntb[20],pow10[20],f[20];
    inline void solve(LL R,LL *cnt)
    {
    	for(int i=0;i<=9;i++) cnt[i]=0;
    	int num[20]={0},len=0;
    	while(R) { num[++len]=R%10; R/=10; }
    	for(int i=len;i>=1;i--)
    	{
    		for(int j=0;j<10;j++) cnt[j]+=f[i-1]*num[i];
    		for(int j=0;j<num[i];j++) cnt[j]+=pow10[i-1];
    		LL lst=0;
    		for(int j=i-1;j>=1;j--) lst=lst*10+num[j];
    		cnt[num[i]]+=lst+1;
    		cnt[0]-=pow10[i-1];
    	}
    }
    int main()
    {
    	#ifdef local
    	freopen("pro.in","r",stdin);
    	#endif
    	pow10[0]=1;
    	for(int i=1;i<=15;i++)
    	{
    		f[i]=f[i-1]*10+pow10[i-1];
    		pow10[i]=pow10[i-1]*10;
    	}
    	while(scanf("%lld %lld",&a,&b)==2&&!(a==0&&b==0))
    	{
    		if(a>b) swap(a,b);
    		solve(b,cntb); solve(a-1,cnta);
    		for(int i=0;i<=8;i++) printf("%lld ",cntb[i]-cnta[i]); printf("%lld
    ",cntb[9]-cnta[9]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    安卓 广播机制
    安卓 活动的启动模式
    安卓 生命周期
    安卓六大布局
    day4-list,列表
    Leetcode 947 移除最多的同行或同列石头
    Leetcode 628三个数的最大乘积
    Leetcode 1584连接所有点的最小费用
    Leetcode 721 账户合并
    Leetcode 103 二叉树的锯齿层序遍历
  • 原文地址:https://www.cnblogs.com/happyZYM/p/11559492.html
Copyright © 2020-2023  润新知