您的位置:首页 > 其它

poj2407 Relatives 欧拉函数基本应用

2013-11-14 16:37 387 查看
题意很简单 就是欧拉函数的定义:

欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) 。题目求的就是φ(n)

根据 通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1,
p2……pn为x的所有质因数,x是不为0的整数

然后利用以下性质变形:

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

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

最后 就是 先把 题目给的 n 进行素因子分解 n=pi^mi*......*pj^mj,求φ(n)其实按照积极函数性质一 φ(n)=φ(pi^mi*)*.....*φ(pj^mj),然后分别求出 φ(pi^mi*)
 根据积极函数的性质二    φ(pi^mi)  =(pi-1)*pi^(mi-1)

#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>

#define ll long long
#define LL __int64
#define eps 1e-8

//const ll INF=9999999999999;

#define inf 0xfffffff

using namespace std;

//vector<pair<int,int> > G;
//typedef pair<int,int> P;
//vector<pair<int,int>> ::iterator iter;
//
//map<ll,int>mp;
//map<ll,int>::iterator p;
//
//vector<int>G[30012];

LL p[100012],m[100012];

int main(void)
{
LL n;
while(cin>>n,n)
{
LL temp=n;
LL cntp=0;
for(ll i=2;i*i<=temp;)
{
if(n%i==0)
{
p[cntp]=i;
LL cntm=0;
while(n%i==0)
{
n/=i;
cntm++;
}
m[cntp++]=cntm;
}
else
i++;
}
if(n>1)
{
p[cntp]=n;
m[cntp++]=1;
}
LL ans=1;
for(LL i=0;i<cntp;i++)
ans*=LL(double(p[i]-1)*pow(double(p[i]),double(m[i]-1)));
cout<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: