hihoCoder挑战赛B题——计数

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值