HDU 4279 2012網路賽Number(數論 尤拉函式結論約數個數)

果7發表於2013-10-05

Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2500    Accepted Submission(s): 692


Problem Description
  Here are two numbers A and B (0 < A <= B). If B cannot be divisible by A, and A and B are not co-prime numbers, we define A as a special number of B.
  For each x, f(x) equals to the amount of x’s special numbers.
  For example, f(6)=1, because 6 only have one special number which is 4. And f(12)=3, its special numbers are 8,9,10.
  When f(x) is odd, we consider x as a real number.
  Now given 2 integers x and y, your job is to calculate how many real numbers are between them.
 

Input
  In the first line there is an integer T (T <= 2000), indicates the number of test cases. Then T line follows, each line contains two integers x and y (1 <= x <= y <= 2^63-1) separated by a single space.
 

Output
  Output the total number of real numbers.
 

Sample Input
2 1 1 1 10
 

Sample Output
0 4
Hint
For the second case, the real numbers are 6,8,9,10.
 

Source
 

題目大意:題目先給你一個定義f(x)表示的是比<=x且不是x的約數並且與x互素的個數,如果f(x)為奇數,那麼x可以算做特殊數,問你a~b之間有多少個特殊數。

  解題思路:除了1之外,沒有與x互素且是x的約數的數字。所以就好辦了。f(x)=x-約數個數-互素個數+1.  由尤拉函式值得到,phi(x)為偶數(x>2)。而一個數的約數的個數是由它素數分解冪數決定的,比如x=e1^p1*e2^p2.....x的約數個數為(p1+1)*(p2+1)*(...)...那麼如果x的約數個數為奇數,p1,p2,..必須都為偶數,那麼x必須為平方數。

下面開始討論f(x)為奇數的情況:
1.x為奇數,約數個數需要為奇數,那麼x為平方數。
2.x為偶數,約數個數需要為偶數,那麼x不為平方數。
特殊的:f(1)=0,f(2)=0;

下面尋找1~x滿足條件的情況:
a.偶數個數為x/2-1,減去1是因為除去了偶數2
b.偶數平方個數為sqrt(x)/2
c.是奇數,又是平方數個數為sqrt(x)-sqrt(x/2)-1   //平方數-偶數的平方數-奇數1
答案是a-b+c,得到x/2-2+sqrt(x)-sqrt(x)/2-sqrt(x)/2;
可以sqrt(x)分奇偶討論:
res=x/2-2+sqrt(x)%2?1:0;

這個好像只有G++能A掉,用c++WA了無數次。。。

  題目地址:Number

AC程式碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;

__int64 cal(__int64 x)
{
    __int64 ans=0;
    if(x<=5) return ans;
    __int64 p=(__int64)sqrt(x*1.0+0.5);
    ans=(x>>1)-2;
    if(p&1) ans++;
    return ans;
}

int main()
{
    int tes;
    scanf("%d",&tes);
    __int64 a,b;
    while(tes--)
    {
        scanf("%I64d%I64d",&a,&b);
        printf("%I64d\n",cal(b)-cal(a-1));
    }
    return 0;
}



開始是打表寫的,下面是打表的程式碼:
int gcd(int m,int n)
{
    int t;
    while(n)
    {
        t=m%n;
        m=n;
        n=t;
    }
    return m;
}

int main()
{

    int i,j;
    int p[55];
    p[0]=0;
    for(i=1; i<=50; i++)
    {
        int cnt=0;
        for(j=2; j<i; j++)
            if(gcd(i,j)>1&&(i%j!=0))
                cnt++;
        if(cnt&1) p[i]=p[i-1]+1;
        else p[i]=p[i-1];
        cout<<i<<" "<<p[i]<<"  "<<(i-4)/2<<" "<<(int)sqrt(i*1.0)<<" "<<endl;
    }
    return 0;
}



相關文章