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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

牛客小白月赛12 D月月给华华出题 (欧拉函数,数论,线筛)

發布時間:2023/12/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 牛客小白月赛12 D月月给华华出题 (欧拉函数,数论,线筛) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/392/D
來源:牛客網

月月給華華出題
時間限制:C/C++ 2秒,其他語言4秒
空間限制:C/C++ 131072K,其他語言262144K
64bit IO Format: %lld
題目描述
因為月月是個信息學高手,所以她也給華華出了一題,讓他求:
\sum_{i=1}^N\frac{i}{\gcd(i,N)}∑
i=1
N
?

gcd(i,N)
i
?

但是因為這個式子實在太簡單了,所以月月希望華華對N=1,2,...,n各回答一次。華華一臉懵逼,所以還是決定把這個問題丟給你。
輸入描述:
一個正整數n。
輸出描述:
輸出n行,第i行表示N=i時的答案。
示例1
輸入
復制
6
輸出
復制
1
2
4
6
11
11
備注:
1\le n\le 10^61≤n≤10
6

請注意輸出的效率

思路:

最后一步是根據這個歐拉函數的一個得出的:

小于等于n的數中與n互質的數sum和為phi(n) * n/2

phi(x)為歐拉函數

由于題目要求輸出1~n的每一個答案,那么我們從1到n枚舉i當做上式中因子d來計算對每個答案的貢獻即可。

細節見代碼:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;} inline void getInt(int* p); const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ ll phi[maxn]; ll prime[maxn]; int check[maxn]; int tot = 0; void build_phi() {phi[1] = 1ll;memset(check, 0, sizeof(check));for (int i = 2; i < 1000010; ++i){if (!check[i]){prime[tot++] = i;phi[i] = i - 1;}for (int j = 0; j < tot; ++j){if (i * prime[j] > 1000010){break;}check[i * prime[j]] = 1;if (i % prime[j] == 0){phi[i * prime[j]] = phi[i] * prime[j];break;} else {phi[i * prime[j]] = phi[i] * (prime[j] - 1);}}} } ll ans[maxn]; int main() {//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);int n;scanf("%d", &n);build_phi();for (ll i = 1; i <= n; ++i){for (ll j = i; j <= n; j += i){ans[j] += phi[j / i] * (j / i) / 2ll;}}repd(i, 1, n){printf("%lld\n", ans[i] + 1ll );}return 0; }inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }

轉載于:https://www.cnblogs.com/qieqiemin/p/11421643.html

總結

以上是生活随笔為你收集整理的牛客小白月赛12 D月月给华华出题 (欧拉函数,数论,线筛)的全部內容,希望文章能夠幫你解決所遇到的問題。

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