传送门 Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7327   Accepted: 2416 Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms.…
Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6383   Accepted: 2043 Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now…
Longge's problem   Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i,…
Description The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6. (a,b) can be easily found by the Euclidean algorithm. Now Carp is consid…
思路:首先给出几个结论: 1.gcd(a,b)是积性函数: 2.积性函数的和仍然是积性函数: 3.phi(a^b)=a^b-a^(b-1); 记 f(n)=∑gcd(i,n),n=p1^e1*p2^e2……; 则 f(n)=∑d*phi(n/d) (d是n的约数)           =∑(pi*ei+pi-ei)*pi^(ei-1). 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include&…
Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N. …
题意:求∑gcd(i,n),1<=i<=n思路:f(n)=∑gcd(i,n),1<=i<=n可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子.为什么呢?原因如下:1到n中有m个数字和n拥有公共的最大因子p,那么就需要把m*p加入答案中.问题是如何计算m的个数.因为假设某个数i与n的最大公约数为p,那么gcd(i,n) = p,可以得到gcd(i/p,n/p)=1.也就是说,有多少个i,就有多少个i/p与n/p互质.那么显然m即为n/p的欧拉函数φ(n/p). 知…
/** 大意: 计算f(n) = ∑ gcd(i, N) 1<=i <=N. 思路: gcd(i,x*y) = gcd(i,x) * gcd(i, y ) 所以gcd 为积性函数 又因为积性函数的和函数 也是积性函数(具体数学,了解即可) f(n) = f(p1^a1 * p2^a2 * p3^a3*......* pn^an ) = f(p1^a1) * f(p2^a2) * f(p3* a3) ...... 现在我们先单独考虑一个 f(p1^a1) f(p^k)=1*φ(p^k)+ p*φ…
A - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2478 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 &l…
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1959  Solved: 1229[Submit][Status][Discuss] Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). Input 一个整数,为N. Output 一个整数,为所求的答案. Sample Inp…
题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). 输入 一个整数,为N. 输出 一个整数,为所求的答案. 样例输入 6 样例输出 15 题解 欧拉函数 易得知满足gcd(n,x)==i的小于等于n的x的个数为phi(n/i), 并且欧拉函数可以在O(√n)的时间内快速求出.. 于是可以先求出所有n的因子,再用欧拉函数得出答案. 由于因子是成对出现的,所以因子并不需要枚举到n,只需枚举到…
http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2*f[i]-1. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <stack> #include <vector> #inclu…
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][Status][Discuss] Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). Input 一个整数,为N. Output 一个整数,为所求的答案. Sample Inp…
题意 题目链接 Sol 开始用反演推发现不会求\(\mu(k)\)慌的一批 退了两步发现只要求个欧拉函数就行了 \(ans = \sum_{d | n} d \phi(\frac{n}{d})\) 理论上来说复杂度是\(O(n)\)的,但是\(d\)的值十分有限.在\(2^{32}\)内最多的约数也只有1920个. /* */ #include<bits/stdc++.h> #define LL long long #define int long long const int MAXN =…
Relatives AC代码 Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16186   Accepted: 8208 Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relativel…
题意: 求sigma phi(n) 思路: 线性递推欧拉函数 (维护前缀和) //By SiriusRen #include <cstdio> using namespace std; #define maxn 1000005 #define int long long int n,p[maxn+100],s[maxn+100],phi[maxn+100],tot; void Phi(){ for(int i=2;i<=maxn;i++){ if(!s[i])p[++tot]=i,phi…
求 \(\sum\limits_{i=1}^{n}gcd(i,n)\) Solution 化简为 \(\sum\limits_{i|n}^{n}φ(\dfrac{n}{i})i\) 筛出欧拉函数暴力求答案即可 #include <bits/stdc++.h> using namespace std; #define int long long int phi(int n) { int m = floor(sqrt(n + 0.5)), ans = n; for (int i = 2; i &l…
题意: 给你一个欧拉函数值 phi(n),问最小的n是多少. phi(n) <= 100000000 , n <= 200000000 解题思路: 对于欧拉函数值可以写成 这里的k有可能是等于0的,所以不能直接将phi(n)分解质因子.但是可以知道(Pr - 1)是一定存在的,那就直接枚举素数,满足phi(n) % (Pr-1)的都加进去,然后对这些素数进行爆搜...说到底还是暴力啊...想不到什么巧妙的办法了,最后需要注意的是,一遍枚举完各个素数后phi(n)除后还剩now,现在要判断(no…
找出N*N范围内可见格点的个数. 只考虑下半三角形区域,可以从可见格点的生成过程发现如下规律: 若横纵坐标c,r均从0开始标号,则 (c,r)为可见格点 <=>r与c互质 证明: 若r与c有公因子1<b<min(r,c),则(c/b, r/b)在线段(0, 0)(c, r)上,则(c, r)不是可见格点.(充分性) 若r与c互质,显然线段上不存在整点,则(c, r)不是可见格点.(必要性) φ(n)表示不超过n且与n互素的正整数的个数,称为n的欧拉函数值 也就是横坐标增1后纵坐标合…
题目链接:http://poj.org/problem?id=2480 题目大意: 题解: 我一直很欣赏数学题完美的复杂度 #include<cstring> #include<algorithm> #include<cstdio> #include<iostream> #include<cmath> using namespace std; typedef long long ll; <<)+; ll n; int main() {…
链接:http://poj.org/problem?id=3090 题意:在坐标系中,从横纵坐标 0 ≤ x, y ≤ N中的点中选择点,而且这些点与(0,0)的连点不经过其它的点. 思路:显而易见,x与y仅仅有互质的情况下才会发生(0,0)与(x,y)交点不经过其它的点的情况,对于x,y等于N时,能够选择的点均为小于等于N而且与N互质的数,共Euler(N)个,而且不重叠.所以能够得到递推公式aa[i]=aa[i]+2*Euler(N). 代码: #include <iostream> #i…
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2705 题意: 求 sigma(gcd(i,n), 1<=i<=n<2^32) 只有一组数据,很好搞,答案就是sigma(phi(n/d)),直接搜就行了. //STATUS:C++_AC_8MS_11284KB #include <functional> #include <algorithm> #include <iostream> //#i…
题意: 给一个L,求长度最小的全8数满足该数是L的倍数. 分析: 转化为求方程a^x==1modm. 之后就是各种数学论证了. 代码: //poj 3696 //sep9 #include <iostream> #include <algorithm> using namespace std; typedef long long ll; ll L; ll factor[65536]; ll mul(ll x,ll y,ll p) { ll ret=0; while(y){ if(y…
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 撕逼题.不就是枚举gcd==d,求和phi[ n/d ]么. 然后预处理sqrt (n)的阶乘,RE得不行.发现用到了大于sqrt (n)的阶乘. 然后翻看TJ. 发现phi可以现求!就用那个式子.我竟然都忘了! 注意最后剩下的一个大于sqrt (i)的质因数. #include<iostream> #include<cstdio> #include<cstrin…
Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime , y > , z > such that a = xy and b = xz. Input There are several test cases. For each test ,,,. A li…
题目来源:id=2480" style="color:rgb(106,57,6); text-decoration:none">POJ 2480 Longge's problem 题意:求i从1到n的gcd(n, i)的和 思路:首先假设m, n 互质 gcd(i, n*m) = gcd(i, n)*gcd(i, m) 这是一个积性函数积性函数的和还是积性函数 由欧拉函数知识得 phi(p^a) = p^a - p^(a-1) p是素数 a是正整数 得到终于答案f(n)…
思路: 虽然看到题目就想到了用欧拉函数做,但就是不知道怎么做... 当a b互质时GCD(a,b)= 1,由此我们可以推出GCD(k*a,k*b)= k.设ans[i]是1~i-1与i的GCD之和,所以最终答案是将ans[0]一直加到ans[n].当 k*i==j 时,ans[j]=k*euler[i]. 看完题解瞬间领悟:神奇海螺 突然忘记欧拉函数是什么:欧拉函数 代码: #include<cstdio> #include<cstring> #include<cstdlib…
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3087    Accepted Submission(s): 953 Problem Description M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a,…
题意 求$ \sum_{i=1}^n gcd(i,n) $ 给定 $n(1\le n\le 2^{32}) $. 链接 题解 欧拉函数 $φ(x)$ :1到x-1有几个和x互质的数. gcd(i,n)必定是n的一个约数. 若p是n的约数,那么gcd(i,n)==p的有$φ(n/p)$个数,因为要使gcd(i,n)==p,i/p和n/p必须是互质的. 那么就是求i/p和n/p互质的i在[1,n]里有几个,就等价于 1/p,2/p,...,n/p 里面有几个和n/p互质,即φ(n/p). 求和的话,…
题目 传送门:QWQ 分析 题意就是求∑gcd(i, N) 1<=i <=N.. 显然$ gcd(i,n) = x $时,必然$x|n$. 所以我们枚举一下n的约数,对于每个约数x,显然$ gcd(i/x,n/x)=1$ 所以我们计算一下n/x的欧拉函数就ok了. 联赛前刷水题qwq 代码 // #include <bits/stdc++.h> #include <cstdio> #include <cmath> #include <algorithm…