zoukankan      html  css  js  c++  java
  • Bzoj4916: 神犇和蒟蒻

    题面

    传送门

    Sol

    第一问puts("1")
    第二问,(varphi(i^2)=ivarphi(i))
    (phi(n)=sum_{i=1}^{n}ivarphi(i))根据杜教筛推的式子

    [g(1)phi(n)=sum_{i=1}^{n}sum_{d|i}g(d)(frac{i}{d})varphi(frac{i}{d})-sum_{i=2}^{n}g(d)phi(lfloorfrac{n}{d} floor) ]

    (g(i)=i)减号前面原式(=sum_{i=1}^{n}isum_{d|i}varphi(frac{i}{d}))
    (sum_{i|n}varphi(i)=n)所以就是(sum_{i=1}^{n}i^2=frac{n(n+1)(2n+1)}{6})
    跑杜教筛即可

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(1e6 + 1), Zsy(1e9 + 7), yyb(166666668);
    
    IL ll Read(){
        RG ll x = 0, z = 1; RG char c = getchar();
        for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        return x * z;
    }
    
    int prime[_], num;
    ll phi[_];
    map <int, int> Phi;
    bool isprime[_];
    
    IL void Prepare(){
    	isprime[1] = 1; phi[1] = 1;
    	for(RG int i = 2; i < _; ++i){
    		if(!isprime[i]){  prime[++num] = i; phi[i] = (i - 1);  }
    		for(RG int j = 1; j <= num && i * prime[j] < _; ++j){
    			isprime[i * prime[j]] = 1;
    			if(i % prime[j]) phi[i * prime[j]] = 1LL * phi[i] * (prime[j] - 1) % Zsy;
    			else{  phi[i * prime[j]] = 1LL * phi[i] * prime[j] % Zsy; break;  }
    		}
    	}
    	for(RG int i = 2; i < _; ++i) phi[i] = 1LL * phi[i] * i % Zsy, (phi[i] += phi[i - 1]) %= Zsy;
    }
    
    IL ll Sumphi(RG ll n){
    	if(n < _) return phi[n];
    	if(Phi[n]) return Phi[n];
    	RG ll ans = n * (n + 1) % Zsy * (2 * n + 1) % Zsy * yyb % Zsy;
    	for(RG ll i = 2, j; i <= n; i = j + 1){
    		j = n / (n / i);
    		ans -= 1LL * (j + i) * (j - i + 1) / 2 % Zsy * Sumphi(n / i) % Zsy;
    		ans = (ans + Zsy) % Zsy;
    	}
    	return Phi[n] = ans;
    }
    
    int main(RG int argc, RG char* argv[]){
    	Prepare(); RG ll n = Read();
    	printf("1
    %lld
    ", Sumphi(n));
        return 0;
    }
    
    
  • 相关阅读:
    在Android工程中运行Java程序问题
    sklearn.neighbors.kneighbors_graph的简单属性介绍
    python中的“.T”操作
    numpy中关于*和dot的区别
    一个Window Service引发的感想
    项目管理之初步认识
    由敏捷开发中开发认领自己的工作内容的感想
    SQL Server2008 inner join多种方式的实践
    浅谈业务逻辑和技术哪个更重要
    敏捷人生之初步认识
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8298830.html
Copyright © 2011-2022 走看看