【POJ】2480 Longge's problem(欧拉函数)
题目
传送门: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(欧拉函数)的更多相关文章
- poj 2480 Longge's problem [ 欧拉函数 ]
传送门 Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7327 Accepted: 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 ...
- poj 2480 Longge's problem 欧拉函数+素数打表
Longge's problem Description Longge is good at mathematics and he likes to think about hard mathem ...
- 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 ...
- 题解报告:poj 2480 Longge's problem(欧拉函数)
Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...
- 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的因子.为什么呢?原因 ...
- poj 3090 && poj 2478(法雷级数,欧拉函数)
http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2 ...
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
- POJ 2478 Farey Sequence(欧拉函数前n项和)
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1959 Solved: 1229[Submit][ ...
随机推荐
- C# zedgraph利用另一窗口取得的串口数据绘图
C# zedgraph利用另一窗口获得的串口数据绘图第一次用zedgraph,非常不熟悉,网上很多内容看的云里雾里... 这个程序主界面接收串口数据,而另外一个窗口进行实时曲线绘图,要怎么样实现for ...
- python3:cmd运行python脚本,提示 No module named 'xxx'
问题:cmd窗口运行python脚本,报错 C:\Users\xxx\Documents\GitHub\python3\main>python run_test.pyTraceback (mos ...
- ZooKeeper 集群环境搭建 (本机3个节点)
--------------------------------------------------------1.建立目录server1server1/dataDirserver1/dataLogD ...
- 6.1 socket 长连接、短连接
一般情况下,服务器的长连接和短连接不是服务器说了算,而是客户端说了算.因为服务器是给别人提供业务的,一旦连接建立起来之后,服务器端不会主动把连接给close掉. 客户端发送一笔业务,没有关闭连接,然后 ...
- Tomcat问题:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined ,At least one of these environment variable is needed to run this program
一眼就能看出来是jdk的环境有问题,但是用了这么久的jdk一直都配置的好好的,怎么一到Tomcat上就这么矫情了. 最后查解决方案,原来是我的jdk从官网直接下载的,虽然我修改了java_home,但 ...
- StyleCop 是什么,可以帮助团队带来什么价值?
StyleCop 本质上是一个 C# 源代码规则分析器,可以帮助团队成员强制执行一组代码样式和一致性规则. 本文将简述 StyleCop 以及它能为团队带来的价值. 本文内容 StyleCop 是什么 ...
- HDU 4666 最远曼哈顿距离
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4666 关于最远曼哈顿距离的介绍: http://blog.csdn.net/taozifish/ar ...
- test20181016 B君的第一题
题意 分析 考场爆零做法 考虑位数少的一定更小,高位小的一定更少. 然后计算一定位数下不同数字的个数,然后从高到低依次确定数位. 特例:如果确定的高位的后缀出现了x,那么要把x调整到后缀去,这样一定更 ...
- JS前端数据多条件筛选(商品搜索)
有时候也会需要在前端进行数据筛选,增强交互体验.当数据可用的筛选条件较多时,把逻辑写死会给后期维护带来很大麻烦.下面是我自己写的一个简单的筛选器,筛选条件可以根据数据包含的字段动态设置. 仿照京东的筛 ...
- restheart 基本使用
restheart 是一个方便基于mongodb的restapi 开发框架 参考项目 https://github.com/rongfengliang/restheart-docker-compose ...