HDU 4279 Number

本文介绍了一种快速计算区间内RealNumber数量的方法。RealNumber定义为一个数f(x)为奇数的情况,其中f(x)是小于x且不能整除x但与x不互质的正整数的数量。通过观察1到50的数列,发现其规律并给出代码实现。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4279

题意:一个数x的f值f(x)等于小于它的正整数中不能整除x但是又与x不互质的数的个数。如果f(x)
是奇数,那么称x为real number。给定区间[a,b],问该区间有多少个real number?


分析:打表找规律。一下为1-50的答案:

3:0
4:0
5:0
6:1
7:1
8:2
9:3
10:4
11:4
12:5
13:5
14:6
15:6
16:6
17:6
18:7
19:7
20:8
21:8
22:9
23:9
24:10
25:11
26:12
27:12
28:13
29:13
30:14
31:14
32:15
33:15
34:16
35:16
36:16
37:16
38:17
39:17
40:18
41:18
42:19
43:19
44:20
45:20
46:21
47:21
48:22
49:23
50:24
Code:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#define LL long long
#define pb push_back
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=100005;
LL l,r;

LL solve(LL x){
    if(x<6) return 0;
    LL s=(LL)floor((sqrt((double)(x))));
    if(s*s>x) s--;                    //不加这句会WA 不明。。。T_T
    return x/2-s/2+(s+1)/2-2;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        cin>>l>>r;
        LL ans=solve(r)-solve(l-1);
        cout<<ans<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值