CF1717E

该博客主要探讨了如何利用欧拉函数预处理和因数分解来解决数学问题,具体涉及gcd(a,b)的计算,并将其转化为求解互质关系的问题。博主通过C++代码展示了如何高效地计算给定整数n的不同子集之和的互质元素个数,整个过程涉及到了数论中的基本概念和算法,如最小质因子、互质关系和最大公约数的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 思路:

我们假设gcd(a, b) = t,然后求\sum_{a + b + c = n} lcm(c, t) = \sum lcm(c, t) \sum_{a + b = n - c}[gcd(a, b) = t],这里技巧性地构造了\sum_{a + b = n - c}[gcd(a, b) = t],接下来gcd(\frac{a}{t},\frac{b}{t}) = 1,然后转化成欧拉函数,接下来找互质即可,\frac{a}{t} + \frac{b}{t} = \frac{n - c}{t},显然\frac{a}{t}\frac{n - c}{t}互质,因为\frac{a}{t}\frac{b}{t}互质,提不出公共的约数.此时\sum_{a + b = n - c}[gcd(a, b) = t] = \varphi(\frac{n - c}{t})(\frac{n - c}{t} \neq 1).

我们预处理欧拉函数和用埃式筛法预处理每个数的因数,然后枚举n - c的因数t,然后计算即可.

#include <bits/stdc++.h>
#define int long long                                         
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
// #define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x)) 
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f 
#define INF 0x3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 2e5 + 10, MOD = 1e9 + 7;
int n, phi[N], primes[N], cnt, vis[N];
vector<int> factor[N];
void get_euler(int n) {
    phi[1] = 1;
    for(int i = 2; i <= n; ++ i) {
        if(!vis[i]) {
            primes[++cnt] = i;
            phi[i] = i - 1;
        }
        for(int j = 1; j <= cnt && i * primes[j] <= n; ++ j) {
            vis[i * primes[j]] = true;
            if(i % primes[j] == 0) {//最小的质因子
                phi[i * primes[j]] = phi[i] * primes[j];
                break;
            }
            phi[i * primes[j]] = phi[i] * (primes[j] - 1);
        }
    }
    phi[1] = 0;
    for (int i = 1; i <= n; ++i)
    	for (int j = 1; j * i <= n; ++j) factor[i * j].emplace_back(j);
}
int lcm(int a, int b) {
	return a / __gcd(a, b) * b;
}
signed main() {
	get_euler(2e5);
	qio >> n;
	int ans = 0;
	for (int c = 1; c < n; ++c)
		for (int t : factor[n - c])
			(ans += lcm(c, t) * phi[(n - c) / t]) %= MOD;
	qio << ans << "\n";
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值