日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NYOJ 1066 CO-PRIME(数论)

發布時間:2025/3/16 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NYOJ 1066 CO-PRIME(数论) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CO-PRIME

時間限制:1000?ms ?|? 內存限制:65535?KB 難度:3 描述

This problem is so easy! Can you solve it?

You are given a sequence which contains n integers a1,a2……an, your task is to find how many pair(ai, aj)(i < j) that ai and aj is co-prime.

?

輸入
There are multiple test cases.
Each test case conatains two line,the first line contains a single integer n,the second line contains n integers.
All the integer is not greater than 10^5.
輸出
For each test case, you should output one line that contains the answer.
樣例輸入
3 1 2 3
樣例輸出
3

題意:給出n個正整數,求這n個數中有多少對互素的數。

分析:莫比烏斯反演。

此題中,設F(d)表示n個數中gcdd的倍數的數有多少對,f(d)表示n個數中gcd恰好為d的數有多少對,

F(d)=f(n)?(n?%?d?==?0)

??f(d)=mu[n?/?d]?*?F(n)?(n?%d?==?0)

上面兩個式子是莫比烏斯反演中的式子。

所以要求互素的數有多少對,就是求f(1)

而根據上面的式子可以得出f(1)=mu[n]?*?F(n)

所以把mu[]求出來,枚舉n就行了,其中mu[i]為i的莫比烏斯函數。

#include<cstdio> #include<cstring> #include<algorithm> using namespace std;const int MAXN = 1e5 + 10; typedef long long LL; int cnt[MAXN], pri[MAXN], num[MAXN], pri_num, mu[MAXN], vis[MAXN], a[MAXN];void mobius(int n) //篩法求莫比烏斯函數 {pri_num = 0;memset(vis, 0, sizeof(vis));vis[1] = mu[1] = 1;for(int i = 2; i <= n; i++) {if(!vis[i]) {pri[pri_num++] = i;mu[i] = -1;}for(int j = 0; j < pri_num; j++) {if(i * pri[j] > n) break;vis[i*pri[j]] = 1;if(i % pri[j] == 0) {mu[i*pri[j]] = 0;break;}mu[i*pri[j]] = -mu[i];}} }LL get(int x) {return (LL)x * (x-1) / 2; }int main() {mobius(100005);int n;while(~scanf("%d",&n)) {int mmax = 0;for(int i = 1; i <= n; i++) {scanf("%d",&a[i]);mmax = max(mmax, a[i]);}memset(cnt, 0, sizeof(cnt));memset(num, 0, sizeof(num));for(int i = 1; i <= n; i++) num[a[i]]++;for(int i = 1; i <= mmax; i++)for(int j = i; j <= mmax; j += i)cnt[i] += num[j];LL ans = 0;for(int i = 1; i <= mmax; i++)ans += get(cnt[i]) * mu[i];printf("%lld\n", ans);}return 0; }

總結

以上是生活随笔為你收集整理的NYOJ 1066 CO-PRIME(数论)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。