1. 程式人生 > >hdu3501 尤拉函式(或容斥原理(莫比烏斯函式))

hdu3501 尤拉函式(或容斥原理(莫比烏斯函式))

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=3501

題解1:顯然,本體可以用容斥原理,求出每個數的倍數情況,其係數就是莫比烏斯函式。

題解2:對於整數n,如果x(x<n)與n互質,那麼(n-x)也與n是互質的;同理如果x(x<n)與n不互質,那麼(n-x)也與n是不互質的。知道這個之後就可以得出:在0<x<n時,存在這樣的x與n互質的個數假設為num(可以通過尤拉函式求得),那麼所有與n互質的x的和sum=num*n/2.

題解2程式碼如下:

#include  <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 10;
const int MOD = 1000000007;
int primes[maxn],pcnt;
int vis[maxn];
ll n;
void get_prime(int n){
	vis[1] = 1;
	for(int i = 2;i <= n;i++){
		if(!vis[i]) primes[++pcnt] = i;
		for(int j = 1;j <= pcnt && primes[j] * i <= n;j++){
			vis[i * primes[j]] = 1;
			if(i % primes[j] == 0) break;
		}
	}
}
int main()
{
	get_prime(maxn - 10);
	while(~scanf("%lld",&n) && n){
		ll phi = n,tmp = n;
		for(int i = 1;i <= pcnt;i++){
			ll t = primes[i];
			if(t * t > n) break;
			if(n % t == 0) {
				phi = phi / t * (t - 1);
				while(n % t == 0) n/=t;
			}
		}
		if(n > 1) phi = phi / n * (n - 1);
		ll ans = (long long)phi * tmp / 2;
		ans = (tmp * (tmp - 1)) / 2 - ans;
		printf("%lld\n",ans % MOD);
	}
	return 0;
}


相關推薦

0x37 原理函數

int sizeof can pre long break true 容斥 組合 多重集的組合數公式得記下。cf451E就是這個的裸題 #include<cstdio> #include<iostream> #include<cs

hdu3501 函式原理函式

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=3501 題解1:顯然,本體可以用容斥原理,求出每個數的倍數情況,其係數就是莫比烏斯函式。 題解2:對於整

線性篩函式函式

在這裡提供三種線性篩的講解,它們分別是:素數篩,尤拉篩和莫比烏斯篩。 ·篩法正確性的重要理論依據: 上述函式均為積性函式。積性函式的性質為:若f(x)是一個積性函式,那麼對於任意素數a,b,滿足f(ab)=f(a)*f(b) ·一些可愛的要點(有助於理解篩法原理

HDU 6390 GuGuFishtion函式+反演

#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #def

GuGuFishtionhdu 6390 函式+函式

題目: 題意: 設 ,已知m,n,p,求   。 思路: 尤拉函式性質: (p為質數)。 一個數肯定能表示成若干個質數的乘積,因此,設。   (其餘的項上下展開後都可以約掉,因為它們互質) 設 。 設 設

hdu 6053 函式

題意:兩個序列,A,B,A序列給出,Bi<=Ai,問滿足所有區間的gcd l r != 1 的B序列的方案數 思路:列舉B整體的GCD,直接列舉顯然會重複計算,顧使用莫比烏斯進行容斥,單組因子的方案數就是sum (ai/p) 顯然直接列舉時間複雜度為n*m  m=m

線性篩函式函式 模板

#include<bits/stdc++.h> using namespace std; const int MAXN = 1e5+1; int prime[MAXN]; //素數 i

BZOJ 2440 完全平方數函式+

   注意的兩點是二分的時候注意,要選取最小的那個答案,因為19是13個,20也是13個,而很明顯19才是符合答案的。 還有感覺題目給的資料範圍不對,實際比較大,所以二分的時候r大點。 #include<bits/stdc++.h> using nam

BZOJ 2818 Gcdgcd(x,y)為素數/函式/反演

題目連結: BZOJ 2818 Gcd 題意: x∈[1,N],y∈[1,N],gcd(x,y)=素數的有序對(x,y)的對數。 分析: 對於一個素數p,如果gcd(x,y)=p,那麼相當於x

【CF900D】Unusual Sequences 反演

div blog mic names include sin 題意 方案 ace 【CF900D】Unusual Sequences 題意:定義正整數序列$a_1,a_2...a_n$是合法的,當且僅當$gcd(a_1,a_2...a_n)=x$且$a_1+a_2+...

BZOJ4833: [Lydsy1704月賽]最小公倍佩爾數min-max&反演線性多項式多個數求LCM

4833: [Lydsy1704月賽]最小公倍佩爾數 Time Limit: 8 Sec  Memory Limit: 128 MBSubmit: 240  Solved: 118[Submit][Status][Dis

Codeforces 548 E Mike ans Foam (與質數相關的多半會用到函式)

題面 Description Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n kinds of beer at Rico's numbered from

A - Eddy's愛好 HDU - 2204 -函式-原理

A - Eddy's愛好  HDU - 2204  題意:要你輸出1到n中,能夠表示成a^b的數,a,b都是大於0的整數的個數,其中b大於1。 思路:算一下 1-n中每個數 進行大於1的次方  不超過n 的個數。會有重複,利用莫比烏斯函式係數

codeforces 839D (推公式+原理/函式

Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the

HDU 6053 TrickGCD 【定理】【函式

Problem Description You are given an array A , and Zhu wants to know there are how many different array B satisfy the following co

【 hdu 6053】 TrickGCD 【數論 + 函式

Problem Description You are given an array A , and Zhu wants to know there are how many different array B satisfy the following co

hdu6053 TrickGCD 函式 原理

TrickGCD Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 2930    Accepted S

BZoj 2301 Problem b定理+反演

2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MB Submit: 7732  Solved: 3750 [Submit][Statu

函式函式的單個值的快速求法

直接根據定義求即可,複雜度為(n)−−−√(n) 題目:莫比與尤拉 AC程式 #include<cstdio> #define ll int using namespace std

線性篩&函式&函式

一: 莫比烏斯反演: vijos1889 描述 小島: 什麼叫做因數分解呢? doc : 就是將給定的正整數n, 分解為若干個素數連乘的形式. 小島: 那比如說 n=12 呢? doc : 那麼就是 12 = 2 X 2 X 3 呀.