URAL
来源:互联网 发布:劫的面具淘宝 编辑:程序博客网 时间:2024/05/22 13:19
题目:求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K 个互不相等的 B 的整 数次幂之和
思路:可以看成2进制的情况,找到第一个大于1的位置pos,从pos开始以后的数的01就可以随意组合了
dp[i][j]:2进制的情况下,长度为i的情况下,有j个1的数的个数
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<ctime>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3fint dp[35][35],dight[35];void Init(){ mm(dp,0); dp[0][0]=1; for(int i=1;i<=31;i++){ dp[i][0]=dp[i][i]=1; for(int j=1;j<i;j++) dp[i][j]=dp[i-1][j]+dp[i-1][j-1]; }}int solve(int n,int K,int base){ if(n<0) return 0; int len=0; do{ dight[++len]=n%base; n/=base; }while(n>0); int res=0,tot=0; for(int i=len;i>=1;i--){ if(dight[i]==1){ res+=dp[i-1][K-tot]; tot++; if(tot>K) break; } else if(dight[i]>1){ res+=dp[i][K-tot]; break; } } if(tot==K) res++; return res;}int main(){ int X,Y,K,B; Init(); while(~scanf("%d%d%d%d",&X,&Y,&K,&B)){ int ans1=solve(Y,K,B); int ans2=solve(X-1,K,B); printf("%d\n",ans1-ans2); } return 0;}
阅读全文
0 0
- URAL
- 【ural】
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- Max sum HUD1003(dp)
- poj 2456:Aggressive cows
- linear regression (依据斯坦福大学网上机器学习视频)
- OrmLite-更符合面向对象的数据库操作方式
- 【总结】常用正则表达式集合
- URAL
- 读取json
- poj1163
- HDU 6183 Color it [CDQ分治+线段树+位压缩]
- Linux系统目录说明
- 洛谷P2125图书馆书架上的书 题解报告
- 天梯赛L2-014. 列车调度
- 使用 jks 签名包 签名失败问题
- 刘轩心理课-五种有损我们智力的习惯!