hihoCoder挑战赛B题——计数
2015-06-15 13:44
351 查看
hihoCoder挑战赛B题——计数
题目链接:http://hihocoder.com/problemset/problem/1178注意点
1、在[L,R]之间的i可能被几种x xor n*x的方式被表示,所以动态数组(bool类型)来保存是否已经被计算过;用set更好2、注意变量的范围选取合适的变量类型;比如这里R最大为10的7次方,最多24位,如果N也是24位,计算int temp = i^(i*n)时会越界。
但是如果我们限制i*n最大24位,那么不会出现越界问题。
但更好的方法是,用long long这样就省事很多了。
可执行代码
#include<iostream> #include<fstream> using namespace std; int help(int n,int L,int R,bool * count) { int k = 0; int RR = R; while(RR!=0) { RR = RR/2; k++; } int max = (1<<k)-1; int result = 0; //注意int的越界问题!!!! for(int i = 0;i*n<=max;i++) { int temp = i^(i*n); if(temp>=L && temp<=R) if(count[temp] == false) { count[temp] = true; result++; } } return result; } int main() { //ifstream cin("input.txt"); int n,L,R; cin>>n>>L>>R; int result; bool * count = new bool[R+1]; for(int i = L;i<=R;i++) count[i] = false; result = help(n,L,R,count); cout<<result<<endl; delete[] count; return 0; }
相关文章推荐
- Root Guard - CCIE之Switching篇
- 8 String to Integer (atoi)
- 6.15 喜刷刷 1 oj c 矩形的运算符重载
- (四)SDN 控制器 NOX 源码分析之—— DSO-deployer
- github完美教程
- Grunt学习使用
- SVN: revert all command
- Spring-boot & spring.security
- WIN8/WIN10更改用户文件夹名称
- 当你输入一个网址的时候,实际会发生什么?
- 自定义导航栏和状态栏
- RPM安装时"Header V3 DSA signature: NOKEY, key ID*****"错误的解决方法
- IOS通知机制(NSNotification)
- redis cluster概览
- ajax分页插件 支持自定义
- power designer创建PDM图sql manager创建数据库
- Storm中设置源节点的输出和目的节点的输入具体步骤
- c#FileStream文件读写(转)
- 揭秘:三国时能令诸葛亮自叹不如的奇才是谁?
- 实践总结 - 不可错过的Angular应用技巧