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 ...
随机推荐
- AutoEventWireup="false"
在 Web 服务器控件中,某些事件(通常是 Click 事件)会导致窗体被回发到服务器.HTML 服务器控件和 Web 服务器控件(如 TextBox 控件)中的更改事件将被捕获,但不会立即导致发送. ...
- 工厂方法(Factory Method)模式
一.工厂方法(Factory Method)模式 工厂方法(FactoryMethod)模式是类的创建模式,其用意是定义一个创建产品对象的工厂接口,将实际创建工作推迟的子类中. 工厂方法模式是简单工厂 ...
- Fedora 20 创建桌面快捷方式
创建desktop文件 sudo touch /usr/share/applications/sublime.desktop 添加内容 [Desktop Entry] Encoding=UTF-8 N ...
- 基础学习总结(六)--getContentRolver()的应用、内容监听者ContentObserver
ContentResolver / getContentResolver() 外界的程序通过ContentResolver接口可以访问ContentProvider提供的数据,在Activity当 ...
- 【原】Infragistics.Win.UltraWinGrid.UltraGrid 增加行号
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayo ...
- Ubuntu14.04忘记root密码的解决方法
电脑20多天没用忘记密码了,下面是在网上找到的一个解决办法,其它的和这个也大概相同.因为其中有些缺漏,没能给我解决问题.通过分析最终问题还是解决了,现解决方案的关键点记录一下.希望能方便到其它人. 1 ...
- Ubuntu 在未知root密码的情况下修改root密码
一, 开机按 F12 (或长按Shift), 进入GRUB界面. 二, 在 recovery mode 按e Ubuntu, Linux 3.5.0-17-generic (恢复模式) (或recov ...
- CentOS 普通用户设置sudo权限
1.先切换到root用户下,输入命令 su 2.添加sudo文件的写权限,命令是: chmod u+w /etc/sudoers 3.编辑sudoers文件 vi /etc/sudoers 找到 ro ...
- centos 普通用户添加sudo权限
本文介绍下,在centos中为普通用户添加sudo权限的方法,供大家学习参考. 在centos中为普通用户增加sudo权限的简单方法,大家参考下. 1,修改/etc/sudoers文件,必须为visu ...
- 1107. Social Clusters (30)
When register on a social network, you are always asked to specify your hobbies in order to find som ...