• 牛客练习赛76 F phi and phi (莫比乌斯反演)


    题目链接:https://ac.nowcoder.com/acm/contest/10845/F

    如图:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    const int maxn = 1000010; 
    const int M = 1000000007;
    
    int n, m;
    int ans[maxn];
    
    int is[maxn], prime[maxn], phi[maxn];
    
    ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }
    
    int main(){
    	n = read();
    	is[1] = phi[1] = 1;
    	for(int i = 2 ; i <= n ; ++i){
    		if(!is[i]){
    			prime[++m] = i;
    			phi[i] = i - 1;
    		}
    		for(int j = 1 ; j <= m && i * prime[j] <= n ; ++j){
    			is[i * prime[j]] = 1;
    			if(i % prime[j] == 0){
    				phi[i * prime[j]] = phi[i] * prime[j];
    				break;
    			} else {
    				phi[i * prime[j]] = phi[i] * (prime[j] - 1); 
    			}
    		}
    	}
    	
    	for(int i = 1 ; i <= n ; ++i){
    		int sum = 0;
    		for(int j = i ; j <= n ; j += i){
    			sum = (sum + phi[j]) % M;
    			ans[j] = (ans[j] + 1ll * sum * sum % M * phi[i] % M) % M;
    			ans[j + i] = ((ans[j + i] - 1ll * sum * sum % M * phi[i] % M) % M + M) % M;
    		}
    	}
    	
    	for(int i = 1 ; i <= n ; ++i){
    		ans[i] = (ans[i] + ans[i - 1]) % M;
    		printf("%d
    ", ans[i]);
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    [转载] $CF652B$ 题解
    [转载] $Luogu$ $P4951$ 题解
    luoguP3723 HNOI2017 礼物
    动态dp学习笔记
    noip级别模板小复习
    noip2017简要题解。
    noip考前抱佛脚 数论小总结
    HDU 6071 Lazy Running
    POI2012 ODL-Distance
    [NOI2002]荒岛野人
  • 原文地址:https://www.cnblogs.com/tuchen/p/14320431.html
Copyright © 2020-2023  润新知