您的位置:首页 > 其它

HDU 4279 Number(数论)

2014-12-13 22:44 337 查看
题目:

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

Simple Input:

2

1 1

1 10

Simple Output:

0

4

Hint:For the second case, the real numbers are 6,8,9,10

数论基础理论:欧拉函数

φ函数的值  通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数。φ(1)=1(唯一和1互质的数(小于等于1)就是1本身)。 (注意:每种质因数只一个。比如12=2*2*3那么φ(12)=12*(1-1/2)*(1-1/3)=4

若n是质数p的k次幂,φ(n)=p^k-p^(k-1)=(p-1)p^(k-1),因为除了p的倍数外,其他数都跟n互质。

设n为正整数,以 φ(n)表示不超过n且与n互

素的正整数的个数,称为n的欧拉函数值,这里函数

φ:N→N,n→φ(n)称为欧拉函数。

欧拉函数是积性函数——若m,n互质,φ(mn)=φ(m)φ(n)。

特殊性质:当n为奇数时,φ(2n)=φ(n), 证明与上述类似。

题目分析:这里用到一个结论:欧拉函数在n>2时,值都为偶数。

我们设f(x)=x-约数个数-互质数个数+1。

当x=1或2时,f(x)=0.

当x>2时,

若x为平方数,f(x)=x-奇-偶+1,f若(x)为奇数,则x必为奇数;

若x不为平方数,f(x)=x-偶-偶+1,若f(x)为奇数,则x必为偶数。

F(x)的值为[3,x]中,其值为奇数平方数+偶数非平方数的个数和,即偶数个数-偶数^2的个数+奇数^2的个数。

而偶数个数为 x/2-1,-1是为了把2减掉。偶数^2个数为 sqrt(x)/2,奇数^2个数为 ( sqrt(x)-(sqrt(x)/2) )-1,这里-1是为了把1减掉。

这题巨坑啊,用C++提交11次wa,用相同的代码G++一次就过,无语了。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
long long f(long long x)//计算小于等于n的real number的个数
{
if(x<=4)
return 0;
long long t=(long long)sqrt(x);
long long ans=(x-4)/2;//大于4的偶数的个数
if(t%2==0)
return ans;
else return ans+1;
}
//大于4,而且不是偶数的平方数的偶数是real number
//奇数的平方的奇数是real number
int main()
{
int T;
long long n,m;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&n,&m);
long long res;
res=f(m)-f(n-1);
printf("%I64d\n",res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: