欧拉函数 codeforces 776E - The Holmes Children

欧拉函数

一、定义。

对φ(N)的值,我们可以通俗地理解为小于N且与N互质的数的个数(包含1)


二、求法。

对于一个正整数N的素数幂唯一分解N=P1^a1*P2^a2*...*Pn^an.

N的素因子有P1,P2,....Pn

[1,n]中任意数x,若x和N含有共同的素因子Pi,则x不与n互质。

故用容斥原理可以去掉这部分数。

例如36=2^2 * 3^2,36的素因子:2,3

φ(36) = 36 - 36/2 - 36/3 +36/(2*3)

          = 36 [ 1 - 1/2 - 1/3 +1/(2*3) ]

          = 36 (1-1/2) (1-1/3)

对任意的n都能推导出φ(N)=N*(1-1/P1)*(1-1/P2)*...*(1-1/Pn).

可以在O(sqrt(n))的时间内求出一个数N的欧拉函数值.


三、性质:

1. 对于素数p, φ(p)=p-1,对于对两个素数p,q φ(pq)=pq-1
     欧拉函数是积性函数,但不是完全积性函数.
     即φ(mn)=φ(n)*φ(m)只在(n,m)=1时成立.

2. 设N为正整数, N = ∑φ(d|N)

     即N的所有因子d的欧拉函数之和 = N

四、模板

1. 求N的欧拉函数值

ll oula(ll x)  
{  
    ll ans=x;  
    ll t=sqrt(x)+1;  
    for(int i=2;i<=t&&i<=x;i++)  
    {  
        if(x%i==0)ans=ans/i*(i-1);  
        while(x%i==0)x/=i;  
    }  
    if(x>1)ans=ans/x*(x-1);  
    return ans;  
}

2. 求[1,N]欧拉函数值打表

素数筛选法,素数i的倍数j,则根据公式phi[j]一定需要乘上(1-1/i)

下面打表1e7的数据量需要650ms

int p[10001000],M=10000100;
void phi_table(){
    memset(p,0,sizeof(p));
    p[1]=1;
    for(int i=2;i<=M;i++)if(!p[i])//p[i]没有被更新,说明i是素数
    {
        for(int j=i;j<=M;j+=i){ //素数的倍数j
            if(!p[j]) p[j]=j;
            p[j]=p[j]/i*(i-1);
        }
    }
}

【例题】:codeforces 776E - The Holmes Children

The Holmes children are fighting over who amongst them is the cleverest.

Mycroft asked Sherlock and Eurus to find value of f(n), where f(1) = 1 and for n ≥ 2f(n) is the number of distinct ordered positive integer pairs (x, y) that satisfy x + y = n and gcd(x, y) = 1. The integer gcd(a, b) is the greatest common divisor of a and b.

Sherlock said that solving this was child's play and asked Mycroft to instead get the value of . Summation is done over all positive integers d that divide n.

Eurus was quietly observing all this and finally came up with her problem to astonish both Sherlock and Mycroft.

She defined a k-composite function Fk(n) recursively as follows:

She wants them to tell the value of Fk(n) modulo 1000000007.

Input

A single line of input contains two space separated integers n (1 ≤ n ≤ 1012) and k (1 ≤ k ≤ 1012) indicating that Eurus asks Sherlock and Mycroft to find the value of Fk(n) modulo 1000000007.

Output

Output a single integer — the value of Fk(n) modulo 1000000007.

Example
Input
7 1
Output
6
Input
10 2
Output
4
Note

In the first case, there are 6 distinct ordered pairs (1, 6)(2, 5)(3, 4)(4, 3)(5, 2) and (6, 1) satisfying x + y = 7 and gcd(x, y) = 1. Hence, f(7) = 6. So,F1(7) = f(g(7)) = f(f(7) + f(1)) = f(6 + 1) = f(7) = 6.

【分析】:

题意中f(n) 表示满足x+y=n且x,y互质的(x,y)的对数。只需满足x与n互质,即欧拉函数。

函数g(n) = n 恒成立。


试着写写前面的几项,能找到规律。Fk(n) = fff...f(n)    其中有(k+1)/2个f 复合

欧拉函数值总满足 f(n) < n ,所以很快就能复合到f(1)或者复合(k+1)/2次。

【代码】:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll oula(ll x)
{
    ll ans=x;
    ll t=sqrt(x)+1;
    for(int i=2;i<=t&&i<=x;i++)
    {
        if(x%i==0)ans=ans/i*(i-1);
        while(x%i==0)x/=i;
    }
    if(x>1)ans=ans/x*(x-1);
    return ans;
}
int main()
{
    ll n,k;
    while(cin>>n>>k)
    {
        k=(k+1)/2;
        while((k--)&&n>1)
            n=oula(n);
        printf("%lld\n",n%1000000007);
    }
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值