您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: