hdu 4279 Number (找规律)

http://acm.hdu.edu.cn/showproblem.php?pid=4279

题意:

给出a,b两个数,1<=a<=b 如果a,b不互质,且a%b != 0则说明a是b的特殊数字f[i]表示i拥有的特殊数字的个数,如果f[i]是奇数,那么i就是 real numbers

给出区间[x,y]求区间内的 real numbers的数量。

思路:

打标找规律,真心不好发现这规律;大于12之后 d[x] = x/2 -1或者d[x] = x/2 - 2;(d[x]表示1到x拥有的 real numbers的数量)发现在遇到平方数是转变原来-1变为-2,否则-2变为-1。  p^2 <= k < (p + 1)^2则数量为:p为奇数-1 偶数-2;

Ps:这里遇到一个很纠结的问题sqrt(x)如果不转换x为浮点型,C++出现编译错误,而G++就AC,而如果转化为浮点型c++wa..无语了。。

View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>

#define CL(a,num) memset((a),(num),sizeof(a))
#define iabs(x)  ((x) > 0 ? (x) : -(x))
#define Min(a,b) (a) > (b)? (b):(a)
#define Max(a,b) (a) > (b)? (a):(b)

#define ll __int64

#define MOD 100000007
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)
#define test puts("<------------------->")
#define maxn 100007
#define M 100007
#define N 107
using namespace std;
//freopen("din.txt","r",stdin);
int f[20] = {0,0,0,0,0,0,1,1,2,3,4,4,5};

ll getR(ll x){
    if (x <= 12) return f[x];
    ll tmp = x/2;
    ll tp = (ll)sqrt(1.0*x);
    if (tp&1) tmp -= 1;
    else tmp -= 2;
    return tmp;
}
int main(){
    ll a,b;
    int t;
    scanf("%d",&t);
    while (t--){
        scanf("%I64d%I64d",&a,&b);
        printf("%I64d\n",getR(b) - getR(a - 1));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值