当前位置:网站首页>[2016 CCPC Hangzhou j] just a math problem (Mobius inversion)
[2016 CCPC Hangzhou j] just a math problem (Mobius inversion)
2022-07-19 03:40:00 【Messy wind】
The question
T T T Group input , Give a positive integer at a time n n n, Definition f ( k ) f(k) f(k) by k k k The number of quality factors , g ( k ) = 2 f ( i ) g(k) = 2 ^ {f(i)} g(k)=2f(i), seek
∑ i = 1 n g ( i ) \sum_{i = 1} ^ {n} g(i) i=1∑ng(i)
1 ≤ T ≤ 50 , 1 ≤ n ≤ 1 0 12 1 \le T \le 50, 1 \le n \le 10 ^ {12} 1≤T≤50,1≤n≤1012
analysis :
First 2 f ( i ) 2 ^ {f(i)} 2f(i) It's not easy to calculate directly , Consider the meaning of combination , Find out 2 f ( i ) 2 ^ {f(i)} 2f(i) It's from i i i The number of schemes that select several subsets from all the prime factors of , hypothesis i = p 1 α 1 p 2 α 2 ⋯ p k α k i = p_1 ^ {\alpha_1} p_2 ^ {\alpha_2}\cdots p_k ^ {\alpha_k} i=p1α1p2α2⋯pkαk, So we can enumerate i i i All divisors of d d d, Bring the divisor into the Mobius function , Then remove all existence greater than or equal to 2 2 2 Inferior prime factor , So each α i \alpha_i αi Can only take 0 0 0 or 1 1 1, But if there are an odd number of prime factors, the value of Mobius function is negative , So square it , namely
∑ i = 1 n 2 f ( i ) = ∑ i = 1 n ∑ d ∣ i μ 2 ( d ) \sum_{i = 1} ^ {n}2 ^ {f(i)} = \sum_{i = 1} ^ {n} \sum_{d \mid i} \mu ^ 2(d) i=1∑n2f(i)=i=1∑nd∣i∑μ2(d)
Exchange summation order
∑ d = 1 n μ 2 ( d ) ⌊ n d ⌋ \sum_{d = 1} ^ {n} \mu ^ 2(d) \lfloor\frac{n}{d}\rfloor d=1∑nμ2(d)⌊dn⌋
At this point, you can apply Complete square The formula of this problem
∑ i = 1 n μ 2 ( i ) = ∑ i = 1 n ∑ d 2 ∣ i μ ( d ) \sum_{i=1}^{n} \mu^2(i)=\sum_{i=1} ^{n}\sum_{d^2 \mid i} \mu(d) i=1∑nμ2(i)=i=1∑nd2∣i∑μ(d)
Substituting to obtain
∑ i = 1 n ⌊ n i ⌋ ∑ d 2 ∣ i μ ( d ) \sum_{i = 1} ^ {n} \lfloor \frac{n}{i} \rfloor \sum_{d ^ 2 \mid i} \mu(d) i=1∑n⌊in⌋d2∣i∑μ(d)
Exchange summation order
∑ d = 1 n μ ( d ) ∑ i = 1 ⌊ n d 2 ⌋ ⌊ n i × d 2 ⌋ = ∑ d = 1 n μ ( d ) ∑ i = 1 ⌊ n d 2 ⌋ ⌊ ⌊ n d 2 ⌋ i ⌋ \sum_{d = 1} ^ {\sqrt n} \mu(d) \sum_{i = 1} ^ {\lfloor \frac{n}{d ^ 2} \rfloor} \lfloor \frac{n}{i \times d ^ 2} \rfloor = \sum_{d = 1} ^ {\sqrt n} \mu(d) \sum_{i = 1} ^ {\lfloor \frac{n}{d ^ 2} \rfloor} \lfloor \frac{\lfloor \frac{n}{d ^ 2} \rfloor}{i} \rfloor d=1∑nμ(d)i=1∑⌊d2n⌋⌊i×d2n⌋=d=1∑nμ(d)i=1∑⌊d2n⌋⌊i⌊d2n⌋⌋
The latter formula can be divided into blocks directly , Pay attention to optimization when Mobius function is not 0 0 0 Only then calculates the answer , And remember the answers in blocks , Complexity is more metaphysical , This question is for 15 15 15 second .
Code :
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
int cnt;
vector<int> primes(1e6 + 1), mobius(1e6 + 1), block(1e6 + 1);
vector<bool> st(1e6 + 1);
void init(int n) {
mobius[1] = 1;
for (int i = 2; i <= n; i ++) {
if (!st[i]) {
primes[cnt ++] = i;
mobius[i] = -1;
}
for (int j = 0; i * primes[j] <= n; j ++) {
int t = i * primes[j];
st[t] = 1;
if (i % primes[j] == 0) {
mobius[t] = 0;
break;
}
mobius[t] = -mobius[i];
}
}
};
void solve() {
int n;
cin >> n;
int res = 0;
auto sum = [&](int n) {
if (n < 1e6 && block[n]) {
return block[n];
}
int res = 0;
for (int l = 1, r; l <= n; l = r + 1) {
r = n / (n / l);
res = (res + (n / l) * (r - l + 1) % mod) % mod;
}
if (n > 1e6) {
return res;
} else {
return block[n] = res;
}
};
for (int i = 1; i * i <= n; i ++) {
if (mobius[i]) {
res = (res + mobius[i] * sum(n / (i * i)) % mod + mod) % mod;
}
}
cout << res << "\n";
}
signed main() {
init(1e6);
cin.tie(0) -> sync_with_stdio(0);
int T;
cin >> T;
for (int t = 1; t <= T; t ++) {
cout << "Case #" << t << ": ";
solve();
}
}
边栏推荐
- KubeCon + CloudNativeCon Europe 2022
- Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]
- Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis
- sublime基本操作
- [NoSQL] redis high availability and persistence
- Derivation of PCA principal component analysis (dimension reduction) process
- oracle 关闭回收站
- Gnome boxes virtual machine creation and installation
- Use RZ, SZ commands to upload and download files through xshell7
- Neural network learning notes 2.2 -- write a simple convolution neural network image classifier with MATLAB
猜你喜欢

367. Effective complete square (necessary for entry)

Gnome boxes virtual machine creation and installation

GoogLeNet

支持工业级瘦设备4G接入,润和软件DAYU120通过OpenHarmony兼容性测评

The WinRAR command copies the specified folder as a compressed file, and calls the scheduled task for backup.

leetcode162. 寻找峰值
![Theoretical basis of doubledqn and its code implementation [pytoch + pendulum-v0]](/img/f2/4a3cc8e5173789111080915aceb9fd.png)
Theoretical basis of doubledqn and its code implementation [pytoch + pendulum-v0]

About 1000base-t1 1000Base-TX and 100base-t1

MySQL optimized index

Zabbix6.0 monitoring vcenter7.0
随机推荐
[NoSQL] redis configuration and optimization of NoSQL (simple operation)
Rhce8 Learning Guide Chapter 1 installing rhel8.4
leetcode:50. Pow(x, n)
Chengxin University envi_ IDL second week class content: open hdf4 file and read the file, as well as simple data processing and saving + detailed analysis
机器学习库Scikit-Learn(线性模型、岭回归、插入一列数据(insert)、提取所需列、向量机(SVM)、聚类)
Chapter I Introduction
Game theory of catching lice
XX市高中网络拓扑整体规划配置
mysql创建项目研发账号
367. 有效的完全平方数(入门必会)
GoogLeNet
About 1000base-t1 1000Base-TX and 100base-t1
Browser cannot open tensorboard
Gnome boxes virtual machine creation and installation
MySQL master-slave setup
The fifth day of the third question of Luogu daily
从 0 到 1 开展软件测试
374. 猜数字大小(入门 必会)
代理模式——B站动力节点
Solve the error of 0x00000709 when win10 connects to the shared printer