ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) E. The Holmes Children

E. The Holmes Children
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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,如果x+y=n,gcd(x,y)=1,那么这对x,y为函数f(n)做了1 的贡献。

那么我们可以试图反推一下:y=n-x,gcd(x,y)=1,gcd(x,n-x)=1,那这个gcd(x,n-x)=1与gcd(x,n)有什么关系吗?答案是 有关系。我们可以假设如果gcd(x,n)=k,那么n=n'k ,x=x'k  那么gcd(x,n-x)=gcd(x'k,(n'-x')k)=k,与之前的gcd(x,n-x)=1矛盾,所以我们发现gcd(x,y)=1和gcd(x,n)=1。那么f(n)函数就可以理解成求n以内,与n互质的数的个数。我们会发现这就是欧拉函数的定义。所以对于f(n),我们只需要求出phi(n)即可。对于g(n),. Summation is done over all positive integers d that divide n. 这句话的意思就是g(n)=n的所有引述的欧拉函数之和。然而我们可以将某个定义记住:n的所有因数的欧拉函数之和等于n。至此,我们已经知道了g(n),f(n),对于这道题我们就可以很轻松的解决了。

只需要求出(k+1)/2次欧拉函数即可,然后判断一下1,即可解决这道题。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<stack>
#include<queue>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#define eps 1e-8
#define zero(x) (((x>0?(x):-(x))-eps)
#define mem(a,b) memset(a,b,sizeof(a))
#define memmax(a) memset(a,0x3f,sizeof(a))
#define pfn printf("\n")
#define ll __int64
#define ull unsigned long long
#define sf(a) scanf("%d",&a)
#define sf64(a) scanf("%I64d",&a)
#define sf264(a,b) scanf("%I64d%I64d",&a,&b)
#define sf364(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define sf2(a,b) scanf("%d%d",&a,&b)
#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define sff(a) scanf("%f",&a)
#define sfs(a) scanf("%s",a)
#define sfs2(a,b) scanf("%s%s",a,b)
#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)
#define sfd(a) scanf("%lf",&a)
#define sfd2(a,b) scanf("%lf%lf",&a,&b)
#define sfd3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)
#define sfd4(a,b,c,d) scanf("%lf%lf%lf%lf",&a,&b,&c,&d)
#define sfc(a) scanf("%c",&a)
#define ull unsigned long long
#define pp pair<int,int>
#define debug printf("***\n")
#define pi 3.1415927
#define mod 1000000007
const double PI = acos(-1.0);
const double e = exp(1.0);
const int INF = 0x7fffffff;;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
bool cmpbig(int a, int b){ return a>b; }
bool cmpsmall(int a, int b){ return a<b; }
using namespace std;
ll phi(ll n) //求phi(n)模板
{
    ll ret=n;
    for(ll i=2;i*i<=n;i++)
        if(n%i==0)
        {
            ret-=ret/i;
            while(n%i==0) n/=i;
        }
    if(n>1)
        ret-=ret/n;
    return ret;
}
int main()
{
    //freopen("data.in","r",stdin);
    //freopen("data.out" ,"w",stdout);
    ll n,k;
    while(~sf264(n,k))
    {
        k=(k+1)/2;
        while(k--&&n>1)
            n=phi(n);
        printf("%I64d\n",n%1000000007);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值