F(x)
来源:互联网 发布:ubuntu如何安装flash 编辑:程序博客网 时间:2024/05/08 14:15
F(x)
Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5055 Accepted Submission(s): 1880
Problem Description
For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 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 < 109)
For each test case, there are two numbers A and B (0 <= A,B < 109)
Output
For every case,you should output "Case #t: " at first, without quotes. Thet is the case number starting from 1. Then output the answer.
Sample Input
30 1001 105 100
Sample Output
Case #1: 1Case #2: 2Case #3: 13题意:找出i在0到b之间的f(i)小于等于f(a)的数的个数。
思路:数位dp。主要在于状态转移不好想。dp[i][j]表示i位数比j小的数的个数。用递归完成的话就只需要思考边界和状态转移。
边界:
dp[i][j]如果j小于0,显然是dp[i][j]=0的,如果i==0,说明就是0,显然任何数都比0大,所以dp[i][j]对于j>=0的时候dp[i][j]=1,否则dp[i][j]=0。
状态转移:
dp[i][j]+=dp[i-1][j-k*(1<<(i))];j的初始值是sum
完成上述两步推导就能开始写这题了。
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MOD = 1000000007;const int maxn = 200010;int num[30],len;LL dp[30][10000];int a[50];int sum;LL dfs(int pos,LL zhi,int last)//zhi是差值{ if(pos<0) return zhi>=0;//当差值大于等于0时,说明这个数符合要求,+1,否则+0 if(zhi<0) return 0;//当差值小于0时就+0, if(!last&&dp[pos][zhi]!=-1) { return dp[pos][zhi]; } int len=last?num[pos]:9; LL res = 0; for(int i=0; i<=len; i++) { res += dfs(pos-1,(zhi-i*(1<<pos)),last&&(i==len)); } if(!last)dp[pos][zhi]= res; return res;}LL solve(LL n){ len = 0; while(n) { num[len++] = n%10; n /= 10; } return dfs(len-1,sum,1);//用sum一直去减}int main(){ memset(dp,-1,sizeof(dp)); int _; scanf("%d",&_); int g=0; while(_--) { LL n,m; ++g; scanf("%I64d%I64d",&m,&n); sum=0; int l=0; while(m)//算sum { sum+=(m%10)*(1<<l); l++; m=m/10; } printf("Case #%d: ",g); if(sum==0) printf("%d\n",1); else printf("%I64d\n",solve(n)); } return 0;}
0 0
- F[x]
- F(x)
- F(X)
- F(X)
- F(x)
- F(x)
- 已知f(x)1)f(x)dx
- HDU4389:X mod f(x)
- 不必要的F(X)
- hdu 4734 F(x)
- HDU 4734 F(x)
- hdu 4734 F(x)
- HDU 4734 F(x)
- NOJ 865 F(x)
- HDU 4734 F(x)
- HDU 4734 F(x)
- HDU 4734 F(x)
- hdu 4734 F(x)
- 概率统计专项练习
- 底部菜单控件-BottomNavigationView的使用
- 输出A+B的M进制
- Webpack+cnpm学习:第一篇
- Apache Avro简介与操作
- F(x)
- Ubuntu之C/C++编译器安装
- Linux与windows之间文件传输
- JAVA并发编程:并发容器之ConcurrentHashMap(转载)
- SQL server 实验三
- 解析大数据基准测试——TPC-H or TPC-DS
- centos 7.0 nginx 1.7.9成功安装过程
- eclipse利用svn插件导入项目后存在的环境问题处理办法。
- J2EE Specification level