标签:lse ble int 链接 algorithm while math.h math scanf
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=3000005;
long long oula[maxn];
int prime[maxn];
bool vis[maxn];
void L_oula()//用线性筛法打表计算maxn中所有的欧拉函数值,
{
  int top=0;
      oula[1]=1;
      for(int i=2; i<maxn; i++)
      {
            if(!vis[i])
            {
                  prime[top++]=i;
                  oula[i]=i-1;
            }
            for(int j=0; j<top&&prime[j]*i<maxn; j++)
            {
                  vis[prime[j]*i]=1;
                  if(i%prime[j]==0)
                  {
                         oula[i*prime[j]]=oula[i]*prime[j];
                        break;
                  }
                  else
                  {
                        oula[i*prime[j]]=oula[i]*(prime[j]-1);
                  }
            }
      }
}
int main()
{
      int a,b;
      L_oula();
      for(int i=2; i<maxn; i++)
      {
            oula[i]+=oula[i-1];//直接更新存在原欧拉函数的值上,否则爆内存
      }
      while(~scanf("%d %d",&a,&b))
      {
            printf("%I64d\n",oula[b]-oula[a-1]);//区间内的欧拉函数值的和
      }  
      return 0;
}
题目链接
http://120.78.128.11/Problem.jsp?pid=2432
#include<stdio.h>
#include<math.h>
#include<set>
#include<string.h>
using namespace std;
int func(int n)//唯一分解原理的计算单个数的欧拉函数
{
    int ans;
    ans = n;
    for(int i = 2; i*i <= n; i++)
    {
        if(n %i == 0)
        {
            ans = ans / i * (i - 1);
            while(n % i == 0)
                n /= i;
        }
    }
    if(n != 1)
        ans = ans / n * (n - 1);
    return ans;
}
int main()
{
    int n;
    while(scanf("%d", &n) && n)
    {
        printf("%d\n", func(n));
    }
    return 0;
}
标签:lse ble int 链接 algorithm while math.h math scanf
原文地址:https://www.cnblogs.com/1998LJY/p/9328178.html