# C++提高组 思路+代码 2月11日

TralSun 2023-02-11 17:54:43

# 题目一

https://www.lanqiao.cn/problems/2139/learning/

## 思路

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 200;
const int M = 1e9 + 7;
ll ni6, ans;
ll get(int p) {
return (ll)p * (p + 1) % M * (2 * p + 1) % M * ni6 % M;
}
ll qmi(ll a, ll b, ll m) {
ll res = 1ll;
while (b) {
if (b & 1) res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
void solve() {
ni6 = qmi(6, M - 2, M);
int n; cin >> n;
for (int l = 1, r; l <= n;) {
r = n / (n / l);
ans = (ans + (n / l) * ((get(r) - get(l - 1)) % M + M) % M) % M;
l = r + 1;
}
cout << ans;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
solve();
return 0;
}


# 题目二

517. 信息传递 - AcWing题库

## 代码

#include<bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
int n, t, ans = 1000000007;
int fa[N], dis[N];
int get(int x)
{
if (x == fa[x]) return x;
int y = fa[x];
fa[x] = get(y);
dis[x] += dis[y];
return fa[x];
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= n; i++)
{
scanf("%d", &t);
int y = get(t);
if (i != y)
{
fa[i] = y;
dis[i] = dis[t] + 1;
}
else ans = min(ans, dis[t] + 1);
}
printf("%d", ans);
return 0;
}

...全文
117 回复 打赏 收藏 转发到动态 举报

50,585

• 近7日
• 近30日
• 至今

刷题！