篩因子

many sum在這裏插入圖片描述
先枚舉因子,再用因子迭代得到倍數  O(nsqrt(n))O(nsqrt(n))

#include<bits/stdc++.h>
using namespace std;
long long A[2000010],B[2000010];
int main(){
    int n,a,m;
    cin>>n>>a>>m;
    A[1]=a;
    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[j]+=A[i];
        }
    }
    for(int i = 1; i <= n ; i++){
        B[i]^=B[i-1];
    }
    cout<<B[n]<<endl;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章