数位统计DP专题(持续更新~) POJ3208启示录 POJ2282The Counting Problem 蓝桥杯12届国赛C++B组H题 二进制问题

 

第一个问题POJ3208启示录

The number 666 is considered to be the occult “number of the beast” and is a well used number in all major apocalypse themed blockbuster movies. However the number 666 can’t always be used in the script so numbers such as 1666 are used instead. Let us call the numbers containing at least three contiguous sixes beastly numbers. The first few beastly numbers are 666, 1666, 2666, 3666, 4666, 5666…

Given a 1-based index n, your program should return the nth beastly number.

Input

The first line contains the number of test cases T (T ≤ 1,000).

Each of the following T lines contains an integer n (1 ≤ n ≤ 50,000,000) as a test case.

Output

For each test case, your program should output the nth beastly number.

Sample Input

3
2
3
187

Sample Output

1666
2666
66666

题目大意

有连续三个6的数字是魔鬼数,问你第k个魔鬼数是多少。

这个问题是属于数位dp中求满足限制条件的第K个数是多少的问题,对于数位DP问题我们一般都要先进行dp数组的初始化,然后运用试填法求出最终的答案。

#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll dp[50][4];
ll t,n;
void init()
{
	dp[0][0]=1;
	for(int i=0;i<20;i++)
	{
		for(int j=0;j<3;j++)
		{
			dp[i+1][j+1]+=dp[i][j];
			dp[i+1][0]+=dp[i][j]*9;
		 } 
		 dp[i+1][3]+=dp[i][3]*10;
	}
}
int main()
{
	//cout<<dp[3][3]<<endl;
	cin>>t;
	init();
	while(t--)
	{
		cin>>n;
		int m;
		for( m=3;dp[m][3]<n;m++);
			int k=0;
			for(int i=m;i>=1;i--)
			{
				for(int j=0;j<=9;j++)
				{
					ll cnt=dp[i-1][3];
					if(j==6||k==3)
					{
						for(int l=max(3-k-(j==6),0);l<3;l++)
						{
							cnt+=dp[i-1][l];
						}
					}
					if(cnt<n)
					{
						n-=cnt;
					}
					else 
					{
						if(k<3)
						{
							if(j==6)k++;
							else k=0;
						}
						cout<<j;
						break;
					}
				}
			}
		
		cout<<endl;
	}
	return 0;
}

第二个问题 POJ2282The Counting Problem

Given two integers a and b, we write the numbers between a and b, inclusive, in a list. Your task is to calculate the number of occurrences of each digit. For example, if a = 1024 and b = 1032, the list will be

1024 1025 1026 1027 1028 1029 1030 1031 1032


there are ten 0's in the list, ten 1's, seven 2's, three 3's, and etc.

Input

The input consists of up to 500 lines. Each line contains two numbers a and b where 0 < a, b < 100000000. The input is terminated by a line `0 0', which is not considered as part of the input.

Output

For each pair of input, output a line containing ten numbers separated by single spaces. The first number is the number of occurrences of the digit 0, the second is the number of occurrences of the digit 1, etc.

Sample Input

1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0

Sample Output

1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106
16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200
413 1133 503 503 503 502 502 417 402 412
196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471
294 1256 296 296 296 296 287 286 286 247

 

题目大意

给你两个数字 l,r,求 两个数字之间所有数字中0-9每个数字出现的次数。

这个问题属于的是数位dp中求在(l,r)范围内有多少个满足限制的数字。

注意:这里无视在dp数组初始化环节中是无视掉前导0的特殊计算的,把0当作普通的数字进行计算,至于特殊情况会在之后的运算中cut出来,大多数数位dp都有这种特点,我在做这道题的时候推了很久dp数组带0计算情况,还是没推出来。。

 

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=11;
ll l,r;
ll dp[maxn][maxn][maxn];//i位大小的数字以j开头的 k个数字数量 
ll t,n;
ll ans[maxn][2];
ll num[maxn];
int cnt=0; 
inline ll quickpow(ll a,ll long b)
{
    ll res = 1;
    for ( ; b ; a = a * a , b>>=1 )
        if ( 1 & b ) res = res * a;
    return res;
}
void init()
{
	for(int i=0;i<=9;i++)dp[1][i][i] =1;
	for(int i=2;i<=10;i++)
	{
		for(int j=0;j<=9;j++)
		{
			for(int k=0;k<=9;k++)
				for(int l=0;l<=9;l++)
				{
					dp[i][j][k]+=dp[i-1][l][k];
				}
			dp[i][j][j]+=quickpow(10,i-1);
		}
	}
}
void work(ll x,ll ty)
{
	memset(num,0,sizeof(num));
	ll now=x;
	cnt=0;
	while(now)
	{
		num[++cnt]=now%10;
		now/=10;
	}
	//先将小于x位数的数字和求出来
	for(int i=1;i<cnt;i++)
	{
		for(int j=1;j<=9;j++)
		{
			for(int k=0;k<=9;k++)
			{
				ans[k][ty]+=dp[i][j][k];
			}
		}
	} 
	//再用试填法操作当前位置
	for(int i=cnt;i>=1;i--)
	{
		for(int j=0;j<num[i];j++)
		{
			if(j==0&&i==cnt)continue;
			for(int k=0;k<=9;k++)
			{
				ans[k][ty]+=dp[i][j][k];
			}
			
		}
		ans[num[i]][ty]+=x%quickpow(10,i-1)+1;//一定要记住加1切记! 
	} 
	
	 
}
int main()
{
	init();
	while(cin>>l>>r)
	{
		if(l==0&&r==0)break;
		memset( ans , 0 , sizeof ans );
		if(l>r)
		{
			ll t=l;
			l=r;
			r=t;
		}
		work(l-1,1);
		work(r,0);
		cout<<ans[0][0]-ans[0][1];
		for(int i=1;i<=9;i++)
		{
			cout<<" "<<ans[i][0]-ans[i][1];
		}
		cout<<endl;
	}
	return 0;
}

第三个问题 第十二届蓝桥杯C++B组国赛H题

这道题是属于给你范围求多少个数符合标准的题目

问题比较裸 代码先不写了

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值