给定
,以及
数组的递推式:
定义
,
的意思是
能被
整除,或者说
是
的一个因子。
例如
,
,
,
......
const int N = 2e6 + 10;
int n, a_begin, m;
int a[N], b[N];
signed main(){
HelloWorld;
cin >> n >> a_begin >> m;
a[1] = a_begin;
for(int i = 2; i <= N - 10; i ++) a[i] = (a[i - 1] + 7 * i) % m;
for(int i = 1; i <= n; i ++){
for(int j = 1; j * j <= i; j ++){
if(i % j == 0){
b[i] += a[j];
if(j * j != i) b[i] += a[i / j];
}
}
}
int ans = 0;
for(int i = 1; i <= n; i ++) ans = ans ^ b[i];
cout << ans << endl;
return 0;
}
比较容易写出代码,但是时间复杂度为
原因在于计算
数组时枚举每一个
,然后
的时间复杂度找每一个
的因子
优化:使用数倍法(埃氏筛思想)来计算
数组:
反过来思考:不是枚举每一个
找每一个
的因子
,而是对于每一个因子
来枚举它的所有倍数
,然后
for(int i = 1; i <= n; i ++){
for(int j = i; j <= n; j += i) b[j] += a[i];
}
时间复杂度:调和级数求和
总代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define HelloWorld IOS;
const int N = 2e6 + 10;
int n, a_begin, m;
int a[N], b[N];
signed main(){
HelloWorld;
cin >> n >> a_begin >> m;
a[1] = a_begin;
for(int i = 2; i <= n; i ++) a[i] = (a[i - 1] + 7 * i) % m;
for(int i = 1; i <= n; i ++){
for(int j = i; j <= n; j += i) b[i] += b[j];
}
int ans = 0;
for(int i = 1; i <= n; i ++) ans = ans ^ b[i];
cout << ans << endl;
return 0;
}



京公网安备 11010502036488号