1. 程式人生 > >TrickGCD(HDU 6053 莫比烏斯函式的反演)

TrickGCD(HDU 6053 莫比烏斯函式的反演)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

const int MAX = 1e5 + 10;
const int mod = 1e9 + 7;

int n;
long long sum;
int a[MAX];
int ssum[MAX];
int Max, Min;

bool notp[MAX];
int prime[MAX], pnum, mu[MAX];

void Moblus()
{
	memset(notp, 0, sizeof(notp));
	mu[1] = 1;
	for (int i = 2; i < MAX; i++)
	{
		if (!notp[i])
		{
			prime[++pnum] = i;
			mu[i] = -1;
		}
		for (int j = 1; prime[j] * i < MAX; j++)
		{
			notp[prime[j] * i] = 1;
			if (i%prime[j] == 0)
			{
				mu[prime[j] * i] = 0;
				break;
			}
			mu[prime[j] * i] = -mu[i];
		}
	}
	for (int i = 0; i < MAX; i++)
		mu[i] = -mu[i];
}

long long fastpow(long long p, int m)
{
	if (p == 1 || m == 0)
		return 1;
	if (p == 0)
		return 0;
	long long res = 1;
	while (m)
	{
		if (m & 1)
			res = res*p%mod;
		p = p*p%mod;
		m >>= 1;
	}
	return res;
}

int main()
{
	int T, Case = 1;
	Moblus();
	scanf("%d", &T);
	while (T--)
	{
		sum = 0;
		scanf("%d", &n);
		memset(ssum, 0, sizeof(ssum));
		Min = MAX;
		Max = -1;
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &a[i]);
			ssum[a[i]]++;
			if (Max < a[i])
				Max = a[i];
			if (a[i] < Min)
				Min = a[i];
		}
		printf("Case #%d: ", Case++);
		for (int i = 1; i <= Max; i++)
			ssum[i] += ssum[i - 1];

		long long res = 1;
		for (int i = 2; i <= Min; i++)
		{
			res = 1;
			if (mu[i] == 0)
				continue;
			int j = min(i, Max);
			int k = min(i * 2 - 1, Max);
			for (int p = 1;; p++)
			{
				if (ssum[k] - ssum[j - 1] != 0)
					res = res*fastpow(p, ssum[k] - ssum[j - 1]) % mod;
				if (k == Max)
					break;
				j += i;
				k += i;
				if (k > Max)
					k = Max;
			}
			sum = ((sum + mu[i] * res) % mod + mod) % mod;
		}

		printf("%lld\n", sum);
	}
	return 0;
}


相關推薦

TrickGCDHDU 6053 函式

#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> using

hdu 6053 函式容斥

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

HDU 6053 TrickGCD 桶裝+分段 /

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

計蒜客 青雲的機房組網方案函式+樹上dsu

題意 給定一棵 n n n 個節點的樹,每個節點上有一個點權,邊權為均

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

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

C - Visible Trees HDU - 2841 -函式-容斥

C - Visible Trees  HDU - 2841  思路 :被擋住的那些點(x , y)肯定是 x 與 y不互質。能夠由其他座標的倍數表示,所以就轉化成了求那些點 x,y互質 也就是在 1 - m    1 - n 中找互質的對數,容斥

BZOJ 2440 完全平方數函式+容斥

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

hdu6053 多校第二場函式,列舉

之前不知道莫比烏斯反演,看了一波,然後有些許理解,這個題其實就是使用了莫比烏斯函式U的定義,詳細的解題報告這裡說的比較清楚 #include<iostream> #include<cstdio> #include<cmath&

HDU 6053 TrickGCD函式

題意:給出陣列 AA ,問有多個種 BB 陣列滿足所給條件。 思路 設 gcd(b1,...bn) = k (k >= 2),此時 k 對答案的貢獻為 (a1/k)*(a2/k)*(a3/k

HDU 6053 TrickGCD 函式

Description You are given an array A , and Zhu wants to know there are how many different array B

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 6053TrickGCD 【數論 容斥 + 函式

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 2017 多校聯合訓練賽2 1009 6053 TrickGCD 函式

莫比烏斯函式完整定義的通俗表達是: 1)莫比烏斯函式μ(n)的定義域是N 2)μ(1)=1 3)當n存在平方因子時,μ(n)=0 4)當n是素數或奇數個不同素數之積時,μ(n)=-1 5)當n是偶數個不同素數之積時,μ(n)=1 定義域為 [ 1, 50 ] 的莫比烏斯函式值如下:(莫比烏斯函式相關內容摘自

hdu 6053 TrickGCD 篩法+函式+分塊處理

題目連結 題意: 給你n個數字,每個位置的數字可以小於等於a[i],求所有gcd(l,r)都滿足大於等於2的情況數; 思路: 首先,比較好想到的就是列舉gcd,那麼每個ai,都有ai/gcd

GuGuFishtionhdu 6390 尤拉函式+函式

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

HDU6053 TrickGCD函式

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; /* 給定數列

hdu 6134 函式,預處理快速求因子個數

> The Death Star, known officially as the DS-1 Orbital Battle Station, also known as the Death Star I, the First Death Star, Project Stardust internally

icpc預賽徐州: Easy Math 有關函式的數學難題

題意: 求∑mi=1u(in)∑i=1mu(in) 解析: 如果n有個因子是某個素數的平方,那麼根據莫比烏斯函式,答案為0,所以我們考慮其他的情況 設d為n的一個素因子,那麼n/d與d互質,而莫比烏斯函式又是積性函式,所以有: ∑i=1

LCM(i,j)求和 函式

原題: BZOJ2693 題意: 求∑ni=1∑mj=1lcm(i,j)∑i=1n∑j=1mlcm(i,j) O(N)O(N)版本: 遇到lcm首先是換成gcd,Ans=∑ni=1∑mj=1i∗jgcd(i,j)Ans=∑i=1n∑j=1mi∗jg

BZOJ 3930 選數函式+杜教篩

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