POJ 2480】的更多相关文章

题目来源: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)…
题意: 求f(n)=∑gcd(i, N) 1<=i <=N. 分析: f(n)是积性的数论上有证明(f(n)=sigma{1<=i<=N} gcd(i,N) = sigma{d | n}phi(n / d) * d ,后者是积性函数),能够这么解释:当d是n的因子时,设1至n内有a1,a2,..ak满足gcd(n,ai)==d,那么d这个因子贡献是d*k,接下来证明k=phi(n/d):设gcd(x,n)==d,那么gcd(x/d,n/d)==1,所以满足条件的x/d数目为phi(…
题目链接: http://poj.org/problem?id=2480 题目大意:求Σgcd(i,n). 解题思路: 如果i与n互质,gcd(i,n)=1,且总和=欧拉函数phi(n). 如果i与n不互质,那么只要枚举n的全部约数,对于一个约数d,若使gcd(i/d,n/d)互质,这部分的gcd和=d*欧拉函数phi(n/d). 不断暴力从小到大枚举约数,这样就把gcd和切成好多个部分,累加起来就行了. 其实还可以公式化简,不过实在太繁琐了.可以参考金海峰神的解释. 由于要求好多欧拉函数,每次…
传送门 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.…
题目链接: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() {…
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,…
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…
题意 求$ \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). 求和的话,…
这里是枚举每一个最大公约数p,那么最后求的是f(n) = sigma(p*phi(n/p))    phi()为欧拉函数 这里可以试着算一下,然后会发现这个是积性函数的 那么只要考虑每一类质数分开算,最后乘在一起就行了 而对于f(p^k) p为素数的求解可以这样考虑 对于前一个f(p^(k-1)) , 那么f(p^k)相当于把f(p^(k-1)) 中的所有情况都乘上了p ,  然后加上新产生的gcd()=1的情况,这个利用过程中的欧拉函数定理求解 phi(n) = (p1-1)*p1^(k1-1…
题意:求∑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). 知…
思路:首先给出几个结论: 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&…
点击打开链接 //求SUM(gcd(i,n), 1<=i<=n) /* g(n)=gcd(i,n),根据积性定义g(mn)=g(m)*g(n)(gcd(m,n)==1) 所以gcd(i,n)是积性的,所以f(n)=sum(gcd(i,n))是积性的, f(n)=f(p1^a1*p2^a2*...*pn^an)=f(p1^a1)*f(p2^a2)*..*f(pn^an) 求f(p1^a1)就可以了,设d为p1^a1的一个因子,gcd(i,n)的个数为phi(n/d) (gcd(i,n/d)==1…
/** 大意: 计算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*φ…
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. …
可以容易得知,F=sum(p*phi(n/p)).思路就断在这里了... 看过别人的,才知道如下: 由于gcd(i,n*m)=gcd(i,m)*gcd(i,n),所以gcd为积性函数.而积性函数之和为积性函数. 所以F=sum(gcd(i,n))为积性函数.n=p1^k1*p2^k2....所以f(p1^k1)*f(p2^k2)...=F. 而f(p^r)由最初公式知f(p^r)=r*(p^r-p^(r-1))+p^r.代入以上公式即可求得. #include <iostream> #incl…
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:求sigma (gcd (i , j))  1 <= i < j <= n 和POJ 2480类似,如果枚举j,求的话,还是会TLE的... 考虑sigma(gcd (i , n)) = sigma (d * phi[n / d]) d | n. 做法同样是先预处理出phi,然后枚举gcd = d. O(n)预处理,O(1)查询 #i…
A - The Euler function 来源:HDU 2824 计算[a,b]区间内的整数的欧拉函数值,需要掌握单个欧拉函数和函数表的使用. #include <iostream> #include <cstdio> using namespace std; ; typedef long long ll; int phi[MAX_N]; // ll sum_phi[MAX_N]; 若使用前缀和累加,会爆内存(MLE) void phi_table(int n) { // 计算…
题目 传送门: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…
Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798   Special Judge Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets…
Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   Special Judge Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000…
The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286   Accepted: 8603   Special Judge Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a…
Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the…
Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yumm…
Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050   Accepted: 10989 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio…
Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital le…
Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Accepted: 9197 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names t…
题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时则按照横坐标从小到大输出. (0 <= x, y <= 32000) 要求输出等级0到n-1之间各等级的星星个数. 分析: 这道题不难想到n平方的算法,即从纵坐标最小的开始搜,每次找它前面横坐标的值比它小的点的个数,两个for循环搞定,但是会超时. 所以需要用一些数据结构去优化,主要是优化找 横坐…
poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23507   Accepted: 11012 Description The Head Elder of the tropical island of Lagrishan has a problem. A b…
Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9567   Accepted: 3888 Description On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka mo…
Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 10626   Accepted: 2949 Description In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone. You can…