数位dp汇总

参考了一些博客中的题目推荐,然后再补了下数位dp。

首先是最近的一场ccpc网络赛的题。

Palindrome Function

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 730    Accepted Submission(s): 396


Problem Description
As we all know,a palindrome number is the number which reads the same backward as forward,such as 666 or 747.Some numbers are not the palindrome numbers in decimal form,but in other base,they may become the palindrome number.Like 288,it’s not a palindrome number under 10-base.But if we convert it to 17-base number,it’s GG,which becomes a palindrome number.So we define an interesting function f(n,k) as follow:
f(n,k)=k if n is a palindrome number under k-base.
Otherwise f(n,k)=1.
Now given you 4 integers L,R,l,r,you need to caluclate the mathematics expression  Ri=Lrj=lf(i,j)  .
When representing the k-base(k>10) number,we need to use A to represent 10,B to represent 11,C to repesent 12 and so on.The biggest number is Z(35),so we only discuss about the situation at most 36-base number.
 

Input
The first line consists of an integer T,which denotes the number of test cases.
In the following T lines,each line consists of 4 integers L,R,l,r.
( 1T105,1LR109,2lr36 )
 

Output
For each test case, output the answer in the form of “Case #i: ans” in a seperate line.
 

Sample Input
  
  
3 1 1 2 36 1 982180 10 10 496690841 524639270 5 20
 

Sample Output
  
  
Case #1: 665 Case #2: 1000000 Case #3: 447525746
关键是要开一个数组记录每次枚举的数字,这样可以让我们在递归长度到1/2的时候可以记得我们前面枚举过的数字 然后查看是否满足回文串。开四维就行,进制 当前位置 有效长度 是否满足回文 这四维就确定了。
#include<bits/stdc++.h>
using namespace std;
int a[50];
int temp[50];
long long dp[40][50][50][2];//base进制 pos位 len实际长度 status满足还是不满足回文的状态 
long long dfs(int pos,int len, int status,bool limit, int base){
	if(pos<0){
		return status ? base:1;
	} 
	if(!limit&&dp[base][pos][len][status]!=-1)return dp[base][pos][len][status];
	int now = limit ? a[pos]:base-1;
	long long ans = 0;
	for(int i=0;i<=now;i++){
		temp[pos]=i;
		if(i==0&&pos==len){//长度相等并且当前位是0 也是前导零 
			ans+=dfs(pos-1,len-1,status,limit&&i==a[pos],base); 
		}
		else if(pos>=(len+1)/2){
			ans+=dfs(pos-1,len,status,limit&&i==a[pos],base);
		}
		else {
			ans+=dfs(pos-1,len,status&&i==temp[len-pos],limit&&i==a[pos],base);
		}
	}
	if(!limit)dp[base][pos][len][status]=ans;
	return ans;
}
long long solve(int x,int k){
	int pos = 0;
	while(x){
		a[pos++]=x%k;
		x/=k;
	}
	return dfs(pos-1,pos-1,true,true,k);
}
int main(){
	int t;
	scanf("%d",&t);
	memset(dp,-1,sizeof(dp));
	int _c=0; 
	while(t--){
		int l,r,L,R;
		long long ans=0;
		scanf("%d%d%d%d",&l,&r,&L,&R);
		for(int i=L;i<=R;i++){
			ans+=solve(r,i)-solve(l-1,i);
		}
		printf("Case #%d: %lld\n",++_c,ans);
	} 
} 


Balanced Number

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 6169    Accepted Submission(s): 2950


Problem Description
A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot is placed at some digit of the number, the distance from a digit to the pivot is the offset between it and the pivot. Then the torques of left part and right part can be calculated. It is balanced if they are the same. A balanced number must be balanced with the pivot at some of its digits. For example, 4139 is a balanced number with pivot fixed at 3. The torqueses are 4*2 + 1*1 = 9 and 9*1 = 9, for left part and right part, respectively. It's your job
to calculate the number of balanced numbers in a given range [x, y].
 

Input
The input contains multiple test cases. The first line is the total number of cases T (0 < T ≤ 30). For each case, there are two integers separated by a space in a line, x and y. (0 ≤ x ≤ y ≤ 10 18).
 

Output
For each case, print the number of balanced numbers in the range [x, y] in a line.
 

Sample Input
     
     
2 0 9 7604 24324
 

Sample Output
     
     
10 897
 
差不多是个模板,只需要多开一维记录每次枚举的位置作为中轴然后计算就行。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long dp[20][20][2005];     //dp[i][j][k]表示考虑i位数字,支点为j,力矩和为k 
int bit[20];

long long DFS(int pos,int central,int pre,int limit){
    if(pos<0)
        return pre==0;//判断pre是否为0 如果为0 则表示平衡 
    if(pre<0)       //当前力矩为负,剪枝 
        return 0;
    if(!limit && dp[pos][central][pre]!=-1)
        return dp[pos][central][pre];
    int end=limit?bit[pos]:9;
    long long ans=0;
    for(int i=0;i<=end;i++)
        ans+=DFS(pos-1,central,pre+i*(pos-central),limit&&(i==bit[pos]));
    if(!limit)
        dp[pos][central][pre]=ans;
    return ans;
}

long long Solve(long long n){
    int len=0;
    while(n){
        bit[len++]=n%10;
        n/=10;
    }
    long long ans=0;
    for(int i=0;i<len;i++)
        ans+=DFS(len-1,i,0,1);//枚举每一个中轴 
    return ans-len+1;       //除掉全0的情况,00,0000满足条件,但是重复了 
}

int main(){

    //freopen("input.txt","r",stdin);

    long long a,b;
    int t;
    scanf("%d",&t);
    memset(dp,-1,sizeof(dp));
    while(t--){
        scanf("%lld%lld",&a,&b);
        //memset(dp,-1,sizeof(dp));
        printf("%lld\n",Solve(b)-Solve(a-1));
    }
    return 0;
}
这里来个bzoj的题 1799 http://www.lydsy.com/JudgeOnline/problem.php?id=1799 连接
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;
typedef long long ll;
ll dp[20][163][163];
int a[20];
ll dfs(int pos,int sum,int val,int mod,bool limit){
    if(sum-9*pos-9>0) return 0;//最坏的情况,这一位及后面的全部为9都不能达到0
    if(pos==-1) return sum==0 && val==0;
    if(!limit&&dp[pos][sum][val]!=-1) return dp[pos][sum][val];
    int up=limit?a[pos]:9;
    ll ans=0;
    for(int i=0;i<=up;i++){
        if(sum-i<0) break;
        ans+=dfs(pos-1,sum-i,(val*10+i)%mod,mod,limit && i==a[pos]);
    }
    if(!limit)dp[pos][sum][val]=ans;
    return ans;
}
ll solve(ll x){
    int pos=0;
    while(x){
        a[pos++]=x%10;
        x/=10;
    }
    ll ans=0;
    for(int i=1;i<=pos*9;i++){//上限就是每一位都是9枚举他们的权值 
        memset(dp,-1,sizeof dp);
        ll tmp=dfs(pos-1,i,0,i,true);
        ans+=tmp;
    }
    return ans;
}
int main(){
    ll le,ri;
    memset(dp,-1,sizeof dp);
    while(~scanf("%lld%lld",&le,&ri))
        printf("%lld\n",solve(ri)-solve(le-1));
    return 0;
}
/*
1 1000000000000000000
*/

F(x)

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6183    Accepted Submission(s): 2362


Problem Description
For a decimal number x with n digits (A nA n-1A n-2 ... A 2A 1), we define its weight as F(x) = A n * 2 n-1 + A n-1 * 2 n-2 + ... + A 2 * 2 + A 1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there between 0 and B, inclusive, whose weight is no more than F(A).
 

Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
For each test case, there are two numbers A and B (0 <= A,B < 10 9)
 

Output
For every case,you should output "Case #t: " at first, without quotes. The  t is the case number starting from 1. Then output the answer.
 

Sample Input
     
     
3 0 100 1 10 5 100
 

Sample Output
     
     
Case #1: 1 Case #2: 2 Case #3: 13
 
这题让我们记录满足该函数狮子的 不超过f(b)的有几个,首先我们可以根据题目条件大概得到这个函数的最大值,总共就做多10位,2^10=1024很显然很小,因此有一维记录该函数式子的最大值。
dp记录当在pos位置时 还需要all-sum的数值 如果all-sum<0即表示不满足条件 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>

using namespace std;
const int N=1e4+5;
int dp[12][N];
int f(int x)
{
    int ans = 0,temp = 1;
    while(x){
    	ans += temp*(x%10);
		x/=10;
		temp*=2; 
    }
    return ans;
}
int all;
int a[12];
int dfs(int pos,int sum,bool limit)
{
    if(pos==-1) {return sum<=all;}//判断是否满足条件 
    if(sum>all) return 0;
    if(!limit && dp[pos][all-sum]!=-1) return dp[pos][all-sum];//dp记录当在pos位置时 还需要all-sum的数值 如果all-sum<0即表示不满足条件 
    int up=limit ? a[pos] : 9;
    int ans=0;
    for(int i=0;i<=up;i++)
    {
        ans+=dfs(pos-1,sum+i*(1<<pos),limit && i==a[pos]);//不断累加sum 
    }
    if(!limit) dp[pos][all-sum]=ans;
    return ans;
}
int solve(int x)
{
    int pos=0;
    while(x)
    {
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,true);//穿入统计参数 pos sum记录这个函数的当前权重 limit有无限制 
}
int main()
{
    int a,ri;
    int T_T;
    int kase=1;
    scanf("%d",&T_T);
    memset(dp,-1,sizeof dp);
    while(T_T--)
    {
        scanf("%d%d",&a,&ri);
        all=f(a);
        printf("Case #%d: %d\n",kase++,solve(ri));
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值