POJ2480 Longge's problem gcd&&phi
题意简洁明了。做这题主要是温习一下phi的求法。令gcd(i,n)=k,实际上我们只需要求出有多少个i使得gcd(i,n)=k就可以了,然后就转化成了求phi(n/k)的和,但是n很大,我们不可能预处理出所有的phi,但是因为k的个数是O(sqrt(n))级别的,所以我们只需要求出sqrt(n)个数的phi就可以了,我们先预处理出所有的质因子及其个数,然后dfs一下就可以了。
#pragma warning(disable:4996)
#include<cstring>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std; int n;
int cnt[35];
int prime[35];
int tot;
ll ans; void dfs(int step, int phi)
{
if (step == tot){
ans += (ll)phi; return;
}
dfs(step + 1, phi);
phi = phi / prime[step] * (prime[step] - 1);
for (int i = 0; i < cnt[step]; i++){
dfs(step + 1, phi);
}
} int main()
{
while (~scanf("%d",&n))
{
memset(cnt, 0, sizeof(cnt)); tot = 0;
int x = n;
for (ll i = 2; i*i <= x; i++){
if (x%i == 0){
for (; x%i == 0; x /= i) cnt[tot]++;
prime[tot++] = i;
}
if (x == 1) break;
}
if (x != 1) cnt[tot]++, prime[tot++] = x;
ans = 0;
dfs(0, n);
printf("%lld\n", ans);
}
return 0;
}
POJ2480 Longge's problem gcd&&phi的更多相关文章
- POJ2480 Longge's problem
题意 Language:Default Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1064 ...
- poj2480——Longge's problem(欧拉函数)
Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9190 Accepted: 3073 ...
- POJ2480:Longge's problem(欧拉函数的应用)
题目链接:传送门 题目需求: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N ...
- Longge's problem poj2480 欧拉函数,gcd
Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6918 Accepted: 2234 ...
- 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 ...
- Longge's problem
Longge's problem 求\(\sum_{i=1}^ngcd(i,n)\),\(n< 2^{31}\). 解 理解1: 注意式子的实际意义,显然答案只可能在n的约数中,而现在问题变成了 ...
- poj 2480 Longge's problem [ 欧拉函数 ]
传送门 Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7327 Accepted: 2 ...
- 365. Water and Jug Problem (GCD or BFS) TBC
https://leetcode.com/problems/water-and-jug-problem/description/ -- 365 There are two methods to sol ...
随机推荐
- 项目中Service层的写法
截取自项目中的一个service实现类,记录一下: base类 package com.bupt.auth.service.base; import javax.annotation.Resource ...
- 设置textview显示框内容不可编辑不可选择。
f1textview.allowsEditingTextAttributes=NO;////////////设置不可编辑不能用这个,得用下面的一个 textView.editable=NO;//设置可 ...
- 《Linux系统free命令的使用》学习笔记
free命令用于显示当前系统的内存空闲和使用情况,其中包括物理内存,交换分区内存,内核缓冲区内存以及高速缓存,free的参数报错一下: -b ——字节的方式显示内存使用情况 [root@redhat ...
- Cadence Allegro小技巧-从外部文本文件添加文本
菜单“Add->Text”,然后在右侧Options栏设置好合适的Class and Subclass,Text block,然后在布板界面上点击鼠标左键,设置起始点,接着点击鼠标右键,在弹出的 ...
- Mac支付宝插件风波
1.前言 首先我喜欢看一些创业的书,很多书里都会有马云的身影,马云也算是对我有一定的影响,从而我对淘宝也产生了一定的好感.但是关于这次插件事情,我对阿里产生了一些排斥的心里作用.我并不想吐槽淘宝,也不 ...
- 封装鼠标滚轮事件_mousewheel
function mousewheel(obj,fn){ obj.onmousewheel===null ? obj.onmousewheel=fun : obj.addEventListener(' ...
- centos 普通用户添加sudo权限
本文介绍下,在centos中为普通用户添加sudo权限的方法,供大家学习参考. 在centos中为普通用户增加sudo权限的简单方法,大家参考下. 1,修改/etc/sudoers文件,必须为visu ...
- mysql rand随机查询记录效率
一直以为mysql随机查询几条数据,就用 SELECT * FROM `table` ORDER BY RAND() LIMIT 5 就可以了. 但是真正测试一下才发现这样效率非常低.一个15万余条的 ...
- 用nodejs删除mongodb中ObjectId类型数据
mongodb中"_id"下面有个ObjectId类型的数据,想通过这个数据把整个对像删除,费了半天劲终于搞定费话少说上代码 module.exports = function ( ...
- Highcharts条形与柱形同时显示
var chart; $(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'chart_combo' //关联页面元素di ...