codeforces 776E The Holmes Children

目录

  • codeforces 776E The Holmes Children
  • 题意
  • 题解
  • Code

codeforces 776E The Holmes Children

题目传送门

题意

定义\(f(n)\)为满足\(x+y=n\)并且\(gcd(x,y)=1\)的有序正整数对个数,定义\(g(n)=\sum_{d|n}f(\frac{n}{d})\),定义\(F_k(n)\)如下:
\[ F_k(n)=\begin{cases}
f(g(n)) & k=1 \g(F_{k-1}(n)) & k>1且k\%mod=0 \f(F_{k-1}(n)) & k>1且k\%mod=1
\end{cases}\]
给出\(n\),\(k\),求\(F_k(n)\)。\((1\leq n,k \leq 10^{12})\)

题解

首先观察\(f(n)\),可以比较容易地推出\(f(n)=\phi(n)\)。推导过程如下:我们先假设一对\((x,y)\),使得\(x+y=n\)并且\(gcd(x,y)=k\neq1\),于是我们可以推出\(gcd(x,n-x)=k\),\(k\)为\(x\)和\(n-x\)的一个因数,所以\(k\)也是\(n\)的一个因数,那么\(gcd(n,x)=k\),所以我们可以得知所有不满足条件的正整数对\((x,y)\)的\(x\)都不是与\(n\)互质的,所以满足条件的就是和\(n\)互质的那些,那么这个就是求欧拉函数了。
然后我们去看\(g(n)\),可以退出\(g(n)=n\),因为一个数所有因数的欧拉函数之和等于这个数本身。推导过程如下:假设\(n=a*b\),那么\(\phi(b)\)为所有与\(b\)的\(gcd\)为1的数,那么这些数与\(n\)的\(gcd\)为\(a\),所以所有与\(n\)的\(gcd\)为\(a\)的数都会对\(\phi(b)\)作出1的贡献,而由于每个小于\(n\)的数都与\(n\)有一个\(gcd\),所以每个数一定都会作出1的贡献,最后答案就是\(n\)。
知道这两个结论之后,就可以将\(F_k(n)\)变成\(\phi^{\frac{k+1}{2}}(n)\),即求\(\frac{k+1}{2}\)次的欧拉函数,而由于欧拉函数的值求到\(log\)次左右之后就会变成1,所以我们最多求\(log\)次欧拉函数,直到\(n=1\)的时候,就可以直接输出答案了,总的复杂度为\(O(logn*\sqrt{n})\)。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
#define PAUSE printf("Press Enter key to continue..."); fgetc(stdin);
const ll Md=1e9+7;
ll n,k;
/*==================Define Area================*/
ll Euler(ll x) {
    ll res=x;
    for(ll i=2ll;i*i<=x;i++) {
        if(x%i==0) {
            res=res-res/i;
            while(x%i==0) x/=i;
        }
    }
    if(x>1) res-=res/x;
    return res;
}

void Calc() {
    ll c=(k+1)/2;
    ll res=Euler(n);
    c--;
    while(c--) {
        res=Euler(res);
        if(res==1) break;
    }
    printf("%lld\n",res%Md);
}

int main() {
    read(n);read(k);
    Calc();
    return 0;
}

原文地址:https://www.cnblogs.com/Apocrypha/p/9537691.html

时间: 2024-08-01 21:41:28

codeforces 776E The Holmes Children的相关文章

Codeforces Round #400 E. The Holmes Children

题目链接:Codeforces Round #400 E. The Holmes Children 题意: 定义f(1)=1,f(n),n>1的值为满足x+y=n且gcd(x,y)=1的(x,y)个数:定义g(n)=Σd|n f(n/d):定义Fk(n)满足k=1时Fk(n)=f(g(n)),k>1且k mod 2=0时Fk(n)=g(Fk-1(n)),k>1且k mod 2=1时Fk(n)=f(Fk-1(n)) .给出n,k,求Fk(n)mod 1000000007.(1<=n,

CodeForces 776E 数学规律,欧拉

CodeForces 776E 题意:定义f(n)为(x,y)的对数,x和y要满足 x>0, y>0, x+y=n, x与y互质. 定义g(n)为f(x1)+f(x2)+......+f(xk),xi为n的因子. 再定义Fk(n)为     给定n和k,求Fk(n). tags: 好假的题..推理或者找规律,f(n)=phi(n), g(n)=n... #include<bits/stdc++.h> using namespace std; #pragma comment(link

Codeforces 1063D Candies for Children

题目大意 给定整数 $n, k, l, r$,$1\le n, k \le 10^{11}$,$1\le l, r \le n$ . 令 $ m = r - l + 1$,若 $m \le 0$,$m\gets m + n$ . 未知数 $x\in \mathbb{Z}$ 满足 $ 0 \le x \le n$,且满足 $ k \bmod (n + x) = 0$ 且 $m = n$ :或者 $ k \bmod (n + x) \ne 0$ 且 $0 \le k \bmod (n + x) -

Codeforces 400 Div.1+Div.2

A. A Serial Killer Examples Input ross rachel4ross joeyrachel phoebephoebe monicamonica chandler Output ross racheljoey racheljoey phoebejoey monicajoey chandler Input icm codeforces1codeforces technex Output icm codeforcesicm technex 题意:求每天存活的人,下两个人

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

前四题比较水,E我看出是欧拉函数傻逼题,但我傻逼不会,百度了下开始学,最后在加时的时候A掉了 AC:ABCDE Rank:182 Rating:2193+34->2227 终于橙了,不知道能待几天 A.A Serial Killer 题目大意:一开始给你两个字符串,每次给你当前两个串中的一个和一个新的串,用新的串换掉旧的,每次输出当前的串.(次数<=1000) 思路:二逼题 #include<iostream> using namespace std; int main() { s

Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)

题目链接:http://codeforces.com/problemset/problem/450/A ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

Codeforces Round #257 (Div. 2/A)/Codeforces450A_Jzzhu and Children

解题报告 没什么好说的,大于m的往后面放,,,re了一次,,, #include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; struct node { int x,cd; }num[1000000]; int main() { int n,m,c; cin>>n>>m; int i; for(i=0;i&l

Codeforces#543 div2 B. Mike and Children(暴力?)

题目链接:http://codeforces.com/problemset/problem/1121/B 题意 给n个数 最多的对数 其中每一对(i,j)的ai+aj都相等(不知道怎么解释.... 判断的话 大概是4重循环 因为每次都选4个数嘛 两两相加判等 不过会TLE 而且并不知道哪些值用了哪些没用 就可以预处理记录a[i]+a[j]出现的次数 然后排序找最大的就好了 代码有点鬼畜.. 代码如下 #include <cstdio> #include <algorithm> #i

CodeForces 383C Propagating tree

Propagating tree Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 383C64-bit integer IO format: %I64d      Java class name: (Any) Iahub likes trees very much. Recently he discovered an interesting tree