题目

传送门:QWQ

分析

题意就是求∑gcd(i, N) 1<=i <=N.。

显然$ gcd(i,n) = x $时,必然$x|n$。

所以我们枚举一下n的约数,对于每个约数x,显然$ gcd(i/x,n/x)=1$

所以我们计算一下n/x的欧拉函数就ok了。

联赛前刷水题qwq

代码

// #include <bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = ;
ll a[maxn], top=, p[maxn];
int fac(ll x) {
int n=x, flag=;
ll end = sqrt(x);
for(ll i=;i<=end;i++) {
if(x%i==) {
flag=;
a[++top] = i;
if(i*i!=x) a[++top] = x/i;
while(n%i==) n/=i;
}
}
if(x==) flag=;
return flag;
}
ll phi(ll x) {
ll ans = x, end = sqrt(x)+;
for(ll i=;i<=end;i++) {
if(x%i==) {
ans = ans / i * (i-);
while(x%i==) x/=i;
}
}
if(x>) ans = ans / x * (x-);
return ans;
}
void Ph() {
for(int i=;i<maxn-;i++) p[i]=i;
for(ll i=;i<maxn-;i++) {
if(p[i]==i) {
for(ll j=i;j<maxn-;j+=i) {
p[j] = p[j] / i * (i-);
}
}
}
}
int main() {
int n; Ph(); while(scanf("%lld",&n)==) {
if(n==){
printf("1\n"); continue;
}
top=; int q=fac(n);
if(q == ) {
printf("%lld\n",phi(n)+n); continue;
}
sort(a+,a++top);
int pp = top;
ll ans=phi(n)+n;
for(int i=;i<=pp;i++) {
ll qwq;
if(n/a[i] < maxn-) qwq = p[n/a[i]];
else qwq = phi(n/a[i]);
ans += a[i] * qwq;
}
printf("%lld\n",ans);
}
}

【POJ】2480 Longge's problem(欧拉函数)的更多相关文章

  1. poj 2480 Longge's problem [ 欧拉函数 ]

    传送门 Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7327   Accepted: 2 ...

  2. POJ 2480 Longge's problem 欧拉函数—————∑gcd(i, N) 1<=i <=N

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6383   Accepted: 2043 ...

  3. poj 2480 Longge's problem 欧拉函数+素数打表

    Longge's problem   Description Longge is good at mathematics and he likes to think about hard mathem ...

  4. poj 2480 Longge's problem 积性函数

    思路:首先给出几个结论: 1.gcd(a,b)是积性函数: 2.积性函数的和仍然是积性函数: 3.phi(a^b)=a^b-a^(b-1); 记 f(n)=∑gcd(i,n),n=p1^e1*p2^e ...

  5. 题解报告:poj 2480 Longge's problem(欧拉函数)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

  6. POJ 2480 Longge's problem (积性函数,欧拉函数)

    题意:求∑gcd(i,n),1<=i<=n思路:f(n)=∑gcd(i,n),1<=i<=n可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子.为什么呢?原因 ...

  7. poj 3090 &amp;&amp; poj 2478(法雷级数,欧拉函数)

    http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2 ...

  8. BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][ ...

  9. POJ 2478 Farey Sequence(欧拉函数前n项和)

    A - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  10. Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1959  Solved: 1229[Submit][ ...

随机推荐

  1. mysql-5.6.17-win32安装

    下载免安装压缩文件http://dev.mysql.com/downloads/mysql/ 解压到自定义目录,我这里演示的是D:\wamp\mysql\   复制根目录下的my-default.in ...

  2. Python decode和encody

    s = "我今天非常的困" bs = s.encode("utf-8")# 把字符串转化成utf-8格式bytes # bytes 不是给人看的, 给机器用的 ...

  3. pygame资源图片剪裁

    裁剪坟墓 def cropimg(image, region): from cStringIO import StringIO img = Image.open(image) # region = ( ...

  4. 简单的C#爬虫

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  5. Python GIL 系列之再谈Python的GIL

    1. 之前写过一篇<通过实例认识Python的GIL>的文章,感觉有些意犹未尽 2. 这次对例子作了些扩展,进一步的分析GIL对Python程序的影响 2.1 先来看例子: [python ...

  6. ABP .Net Core 调用异步方法抛异常A second operation started on this context before a previous asynchronous operation completed

    1.  问题描述 最近使用ABP .Net Core框架做一个微信开发,同时采用了一个微信开发框架集成到ABP,在微信用户关注的推送事件里调用了一个async 方法,由于没有返回值,也没做任何处理,本 ...

  7. LDA模型应用实践-希拉里邮件主题分类

    #coding=utf8 import numpy as np import pandas as pd import re from gensim import corpora, models, si ...

  8. Spring DI

    一.   Spring DI 依赖注入 利用spring IOC实例化了对象,而DI将实例化的对象注入到需要对象的地方,完成初始化任务. 对象由spring创建,之后再由spring给属性赋值 spr ...

  9. [UE4]虚幻4的智能指针

    虚幻自己实现了一套智能指针系统,为了跨平台. 指针: 占用8个字节,4个字节的Object指针,4字节的引用计数控制器的指针, 引用计数控制器需要12字节, 一个C++的Object指针4字节,一个共 ...

  10. centos安装redis +RedisDesktopManager连接redis

    1.先到Redis官网(redis.io)下载redis安装包 wget http://download.redis.io/releases/redis-5.0.4.tar.gztar xzf red ...