HDOJ 3501 Calculation 2

题目链接

分析:

要求的是小于$n$的和$n$不互质的数字之和...那么我们先求出和$n$互质的数字之和,然后减一减就好了...

$\sum _{i=1}^{n} i[gcd(i,n)==1]=\frac{n\phi(n)}{2}$

考虑$gcd(n,i)=1$,那么必然有$gcd(n,n-i)=1$,然后发现如果把$gcd(n,i)=1$和$gcd(n,n-i)=1$凑到一起会出现$n$,这样的有$\frac{\phi(n)}{2}$对...

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
//by NeighThorn
using namespace std;

const int mod=1e9+7;

int n,ans;

inline int phi(int n){
	int x=n,m=sqrt(n);
	for(int i=2;i<=m;i++)
		if(n%i==0){
			x=1LL*x/i*(i-1)%mod;
			while(n%i==0)
				n/=i;
		}
	if(n>1) x=x/n*(n-1)%mod;
	return x;
}

signed main(void){
	while(scanf("%d",&n)&&n){
		ans=(1LL*n*(n-1)/2)%mod;
		ans-=(1LL*phi(n)*n/2)%mod;
		if(ans<0) ans+=mod;
		printf("%d\n",ans);
	}
	return 0;
}

  



By NeighThorn

时间: 2024-10-04 16:57:46

HDOJ 3501 Calculation 2的相关文章

hdoj 3501 -Calculation 2 (欧拉函数)

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3305    Accepted Submission(s): 1367 Problem Description Given a positive integer N, your task is to calculate the sum of the positi

HDOJ 3501 Calculation 2(欧拉函数拓展——求非互质数和)

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2548    Accepted Submission(s): 1064 Problem Description Given a positive integer N, your task is to calculate the sum of the posit

hdoj 3501 Calculation 2 【欧拉函数】

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2306    Accepted Submission(s): 976 Problem Description Given a positive integer N, your task is to calculate the sum of the positi

hdu 3501 Calculation 2 (欧拉函数)

题目 题意:求小于n并且 和n不互质的数的总和. 思路:求小于n并且与n互质的数的和为:n*phi[n]/2 . 若a和n互质,n-a必定也和n互质(a<n).也就是说num必定为偶数.其中互质的数成对存在.其和为n. 公式证明: 反证法:如果存在K!=1使gcd(n,n-i)=k,那么(n-i)%k==0而n%k=0那么必须保证i%k=0k是n的因子,如果i%k=0那么gcd(n,i)=k,矛盾出现; 所以先求出1--n-1 的和, 再用这个和 减去 上面公式求出来的值. 欧拉函数phi(m)

HDU 3501 Calculation 2(欧拉函数的应用)

HDU 3501 Calculation 2 大意:求1~n之间与n不互质的数的总和. 思路:欧拉函数的应用:先用欧拉函数求出与n互质的总数m,计算m个数的总和,用n的总和减去m的总和就是想要的结果. 1 #include <stdio.h> 2 #define LL __int64 3 4 int eular(int n){ 5 int ret = 1; 6 for(int i = 2; i*i <= n;i++) 7 if(n%i == 0){ 8 n /= i, ret *= i-

HDU 3501 Calculation 2 (欧拉函数应用)

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2989    Accepted Submission(s): 1234 Problem Description Given a positive integer N, your task is to calculate the sum of the posit

HDU 3501 Calculation 2

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3501 解题思路: 小于n与n互质的数的和为Eular(n)×n/2 实现代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MOD=1000000007 ; /* 小于n与n互质的数的和为Eu

HDU 3501 Calculation 2 (欧拉函数)

题目链接 题意 : 求小于n的数中与n不互质的所有数字之和. 思路 : 欧拉函数求的是小于等于n的数中与n互质的数个数,这个题的话,先把所有的数字之和求出来,再减掉欧拉函数中所有质数之和(即为eular(n)*n/2),得到的就是最终结果,所以也是模板题一道. 1 //3501 2 #include <iostream> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #i

HDU 3501 Calculation 2(欧拉函数)

Calculation 2 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Given a positive integer N, your task is to calculate the sum of the positive integers less than N which are not coprime to N. A is s