[kuangbin带我飞]数位DP F(x)

本文介绍了一道关于位运算与数位DP的算法题,详细解析了如何利用位运算特性优化数位DP算法,避免重复计算,提高算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F(x)

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


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
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   5722  5721  5720  5719  5718 
 

Statistic |  Submit |  Discuss |  Note




先学习一个位运算


<<是左移,比如1<<n,表示1往左移n位,即数值大小2的n次方
>>右移,类似左移,数值大小除以2的n次方
题目意思:对于一个数a,一个数b;
求在[0,b]的范围内有多少个数的F函数的值要小于等于FA;
输出数的个数!
解题思路:
对于每一对a,b所求出的FA 显然都是不一样的!
有错误的做法是:用数位DP把每一个数的	F函数值求出来并和FA 比较,好像这样的思路也是可以的,但是你会发现没有办法保存DP[pos][sum],因为这个DP里面保存的res是针对一个FA所得到的结论,在第二组样例中这样的DP显然就不能重复利用了!如果对每一组样例的DP初始化重新计算,这样你的答案会正确。只是显然这样会TLE。
有正确的做法是:每一次数位DP,其sum值保存的为FA-FX,这样就可以重复利用你的DP啦! 
错误代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
    using namespace std;
#define ll long long
int fa;
int DP[20][6000];
int str[20];
int multi(int x){
    int count=1;
    int ans=0;
    while(x){
        ans+=(x%10)*count;
        count<<=1;
        x=x/10;
    }
    return ans;
}
int dfs(int pos,int sum,int limit){
    if(pos<0){
        return sum-fa<=0;
    }
    if(sum>fa)
        return 0;
    if(!limit&&DP[pos][sum]!=-1)
        return DP[pos][sum];
    int res=0;
    int end=limit?str[pos]:9;
    for(int i=0;i<=end;i++){
        res+=dfs(pos-1,sum+i*(1<<pos),limit&&(i==end));
    }

    return res;
}
int solve(int x){
    int len=0;
    while(x){
        str[len++]=x%10;
        x=x/10;
    }
    len--;
    return dfs(len,0,1);
}
int main(){
    int cas;
    int a,b;
    scanf("%d",&cas);
    memset(DP,-1,sizeof(DP));
    for(int ii=1;ii<=cas;ii++){

        scanf("%d%d",&a,&b);
        fa=multi(a);

        printf("Case #%d: %d\n",ii,solve(b));
    }
    return 0;
}<span style="color:#ff0000;">
</span>

正确代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
    using namespace std;
#define ll long long
int fa;
int DP[20][6000];
int str[20];
int multi(int x){
    int count=1;
    int ans=0;
    while(x){
        ans+=(x%10)*count;
        count<<=1;
        x=x/10;
    }
    return ans;
}
int dfs(int pos,int sum,int limit){
    if(pos<0){
        return sum>=0;
    }
    if(sum<0)
        return 0;
    if(!limit&&DP[pos][sum]!=-1)
        return DP[pos][sum];
    int res=0;
    int end=limit?str[pos]:9;
    for(int i=0;i<=end;i++){
        res+=dfs(pos-1,sum-i*(1<<pos),limit&&(i==end));
    }
    if(!limit)
        DP[pos][sum]=res;
    return res;
}
int solve(int x){
    int len=0;
    while(x){
        str[len++]=x%10;
        x=x/10;
    }
    len--;
    return dfs(len,fa,1);
}
int main(){
    int cas;
    int a,b;
    scanf("%d",&cas);
    memset(DP,-1,sizeof(DP));
    for(int ii=1;ii<=cas;ii++){

        scanf("%d%d",&a,&b);
        fa=multi(a);
        //cout<<fa<<endl;
        printf("Case #%d: %d\n",ii,solve(b));
    }
    return 0;
}

### 关于 kuangbin ACM 算法竞赛培训计划 #### 数论基础专题介绍 “kuangbin”专题十四涵盖了数论基础知识的学习,旨在帮助参赛者掌握算法竞赛中常用的数论概念和技术。该系列不仅提供了丰富的理论讲解,还推荐了一本详细的书籍《算法竞赛中的初等数论》,这本书包含了ACM、OI以及MO所需的基础到高级的数论知识点[^1]。 #### 并查集应用实例 在另一个具体的例子中,“kuangbin”的第五个专题聚焦于并查集的应用。通过解决实际问题如病毒感染案例分析来加深理解。在这个场景下,给定一组学生及其所属的不同社团关系图,目标是从这些信息出发找出所有可能被传染的学生数目。此过程涉及到了如何高效管理和查询集合成员之间的连通性问题[^2]。 #### 搜索技巧提升指南 对于简单的搜索题目而言,在为期约两周的时间里完成了这一部分内容的学习;尽管看似容易,但对于更复杂的状况比如状态压缩或是路径重建等问题,则建议进一步加强训练以提高解题能力[^3]。 ```python def find_parent(parent, i): if parent[i] == i: return i return find_parent(parent, parent[i]) def union(parent, rank, x, y): rootX = find_parent(parent, x) rootY = find_parent(parent, y) if rootX != rootY: if rank[rootX] < rank[rootY]: parent[rootX] = rootY elif rank[rootX] > rank[rootY]: parent[rootY] = rootX else : parent[rootY] = rootX rank[rootX] += 1 # Example usage of Union-Find algorithm to solve the virus spread problem. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值