• POJ 2480 Longge's problem 积性函数


    题目来源:POJ 2480 Longge's problem

    题意:求i从1到n的gcd(n, i)的和

    思路:首先假设m, n 互质 gcd(i, n*m) = gcd(i, n)*gcd(i, m) 这是一个积性函数积性函数的和还是积性函数

    由欧拉函数知识得 phi(p^a) = p^a - p^(a-1) p是素数 a是正整数

    得到终于答案f(n) = f(p1^a1)*f(p2^a2)*...*f(pn^an) 当中f(p^a) = a*(p^a-p^(a-1))+p^a

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    typedef long long LL;
    
    const int maxn = 1000010;
    //筛素数 
    int vis[maxn];
    LL prime[maxn];
    
    LL pow_mod(LL a, LL p)
    {
    	LL ans = 1;
    	while(p)
    	{
    		if(p&1)
    		{
    			ans *= a;
    
    		}
    		a *= a;
    
    		p >>= 1;
    	}
    	return ans;
    }
    
    void sieve(int n)
    {
    	int m = sqrt(n+0.5);
    	memset(vis, 0, sizeof(vis));
    	vis[0] = vis[1] = 1;
    	for(int i = 2; i <= m; i++)
    		if(!vis[i])
    			for(int j = i*i; j <= n; j += i)
    				vis[j] = 1;
    }
    
    int get_primes(int n)
    {
    	sieve(n);
    	int c = 0;
    	for(int i = 2; i <= n; i++)
    		if(!vis[i])
    			prime[c++] = i;
    	return c;
    }
    
    int main()
    {
    	int c = get_primes(200000);
    	int cas = 1;
    	int T;
    	LL n, ans;
    	while(scanf("%I64d", &n) != EOF)
    	{
    		ans = 1;	
    		for(int i = 0; i < c && prime[i]*prime[i] <= n; i++)
    		{
    			if(n%prime[i] == 0)
    			{
    				LL sum = 0;
    				while(n%prime[i] == 0)
    				{
    					sum++;
    					n /= prime[i];
    					
    				}
    				ans *= sum*(pow_mod(prime[i], sum)-pow_mod(prime[i], sum-1))+pow_mod(prime[i], sum);
    			}
    		}
    		if(n > 1)
    		{
    			ans *= n-1+n;
    		}
    		printf("%I64d
    ", ans);
    	}
    	return 0;
    }


  • 相关阅读:
    The Snail
    Oil Deposits
    杭电3784(继续xxx定律)
    poj 2395 Out of Hay
    poj 2485 Highways(简单题)
    poj 2560 || 杭电1162
    Rescue
    “中国芯”能抗衡英特尔吗?
    2013,中国计算巨头放眼国际市场
    123063天两度瘫痪:为啥不在淘宝上卖火车票?
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6848079.html
Copyright © 2020-2023  润新知