hdu 4279 Number(找规律)

来源:互联网 发布:linux 监控tomcat日志 编辑:程序博客网 时间:2024/06/13 13:42

题目链接:

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

解题思路:

题目大意:

定义f(x)是大于0小于x中不能被x整除且与x不互质的数的个数,如果f[x]是奇数,那么x就是 real numbers。

给你两个数x与y ,求出在他们之间的real number数的个数。

找规律,打表即可。。。

打表代码:

#include <iostream>#include <cstdio>using namespace std;int gcd(int a,int b){    if(b == 0)        return a;    return gcd(b,a%b);}int main(){    int tmp = 1000;    for(int i = 1; i <= tmp; i++){        int num = 0;        for(int j = 1; j < i-1; j++){            int m = gcd(i,j);            if(m != j && m>1)                num++;        }        if(num%2)            printf("%d\n",i);    }    return 0;}


AC代码:

#include <iostream>#include <cstdio>#include <cmath>using namespace std;typedef long double ld;typedef long long ll;ll solve(ll x){    if(x < 6)        return 0;    ll xx = (ll)sqrt((ld)x);    //这题卡精度卡的比较紧,ll xx = (ll)sqrt(x*1.0);这样写并不能过    //转化为long double才能过。。。    if(xx%2)        return x/2-1;    else        return x/2-2;}int main(){    int T;    scanf("%d",&T);    while(T--){        ll x,y;        scanf("%lld%lld",&x,&y);        printf("%lld\n",solve(y)-solve(x-1));    }    return 0;}




0 0
原创粉丝点击