URAL 1057 Amount of Degrees (数位统计)
来源:互联网 发布:怎样成为淘宝金牌卖家 编辑:程序博客网 时间:2024/04/28 05:32
转载请注明出处,谢谢 http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove
题目:给出一个区间,问这个区间内的数,转换成B进制后,刚好有B个1的的数有多少个。
http://acm.timus.ru/problem.aspx?space=1&num=1057
关于数位统计类问题可以看论文 《浅谈数位类统计问题》
其中的一个思想是转换成树型结构,通过组合数求出i位中有j个1的数目。
然后从高位开始枚举。又有点像数位DP
二进制很好理解,对于B进制的话。如果当前位大于1,那么后面的可以随意选都比原来的数小。
枚举时要统计高位出现了多少1。
#include<iostream>#include<cstring>#include<queue>#include<cstdio>#include<cmath>#include<algorithm>#define N 30#define inf 1<<29#define MOD 2007#define LL long longusing namespace std;int c[32][32]={0};int slove(int n,int b,int k){ int bit[35]={0},len=0,t=n; while(t){ bit[len++]=t%b; t/=b; } int sum=0,tot=0; for(int i=len-1;i>=0;i--){ //当前位大于1,则后面的选出若干位为1,其余为0 if(bit[i]>1){ sum+=c[i+1][k-tot]; break; } else if(bit[i]==1){ if(i>=k-tot) sum+=c[i][k-tot]; if(++tot>k) break; } //由于是闭区间,不要忘了统计原数本身 if(!i&&tot==k) sum++; } return sum;}int main(){ int x,y,k,b; for(int i=0;i<32;i++){ c[i][0]=c[i][i]=1; for(int j=1;j<i;j++) c[i][j]=c[i-1][j]+c[i-1][j-1]; } while(scanf("%d%d%d%d",&x,&y,&k,&b)!=EOF) printf("%d\n",slove(y,b,k)-slove(x-1,b,k)); return 0;}
- URAL 1057 Amount of Degrees (数位统计)
- ural 1057 - Amount of Degrees(数位统计)
- ural 1057 Amount of Degrees(数位统计)
- Ural 1057. Amount of Degrees 数位统计
- Ural 1057(Amount of Degrees-数位统计入门)
- [ACM] ural 1057 Amount of degrees (数位统计)
- URAL 1057 Amount of Degrees(数位统计)
- [数位dp] ural 1057 Amount of Degrees
- ural 1057 Amount of Degrees(数位DP)
- ural 1057 Amount of degrees 【数位dp】
- 【URAL】1057 Amount of Degrees 数位DP
- URAL 1057 Amount of Degrees (数位dp)
- 数位DP-URAL-1057-Amount of Degrees
- 【URAL 1057】 Amount of Degrees 【数位DP】
- 【数位DP】URAL 1057 Amount of Degrees
- URAL 1057 Amount of Degrees(数位dp)
- URAL 1057 Amount of Degrees 数位DP *
- URAL - 1057 Amount of Degrees--数位dp
- 一步一步教你怎么编译GNU ARM GCC 交叉编译器 arm-none-eabi-gcc
- Resharper进阶二:快速定位
- dwr框架简单例子
- SPS中计算值公式函数简介
- xp3系统配置cocos2dx
- URAL 1057 Amount of Degrees (数位统计)
- Android的ADB工具使用
- Hibernate二级缓存
- 【Android游戏开发十五】关于Android 游戏开发中 OnTouchEvent() 触屏事件的性能优化笔记!
- 穷举法迷宫求解简单实现(C)
- 50个Jquery使用技巧让你成为前端大神
- Mootools DOM操作
- fread fwrite fprintf
- Eclipse不可不知的用法