bzoj 2705: [SDOI2012]Longge的问题 歐拉函數
2705: [SDOI2012]Longge的问题
Time Limit: 3 Sec Memory Limit: 128 MB
Submit: 1035 Solved: 669
[Submit][Status]
Description
Input
Output
Sample Input
Sample Output
HINT
【数据范围】
对于60%的数据,0<N<=2^16。
对于100%的数据,0<N<=2^32。
Source
枚舉n的因數k,算出gcd(i,n)==k,即gcd(i/k,n/k)==1,的數個數,即phi(n/k)。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 1100000
#define MAXV MAXN*2
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
typedef long long qword;
inline int nextInt()
{
char ch;
int x=;
bool flag=false;
do
ch=getchar(),flag=(ch=='-')?true:flag;
while(ch<''||ch>'');
do x=x*+ch-'';
while (ch=getchar(),ch<='' && ch>='');
return x*(flag?-:);
} qword n,m;
qword prime[MAXN],topp=-;
bool pflag[MAXN];
int gcd(int x,int y)
{
return (x%y==) ? y : gcd(y,x%y);
}
void init()
{
int i,j;
for (i=;i<MAXN;i++)
{
if (!pflag[i])
{
prime[++topp]=i;
}
for (j=;j<=topp && i*prime[j]<MAXN;j++)
{
pflag[i*prime[j]]=true;
if (i%prime[j]==)
{
break;
}
}
}
}
qword phi(qword x)
{
int i;
qword ret=;
for (i=;i<=topp && prime[i]*prime[i]<=x;i++)
{
if(x%prime[i]==)
{
ret*=prime[i]-;
x/=prime[i];
while (x%prime[i]==)
{
ret*=prime[i];
x/=prime[i];
}
}
}
if (x>)
{
ret*=x-;
}
return ret;
}
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
qword i;
scanf("%d",&n);
qword ans1=,ans2=;
// for (i=1;i<=n;i++)
// ans1+=gcd(i,n);
// printf("%d\n",ans1);
init();
qword l=ceil(sqrt(n));
for (i=;i*i<n;i++)
{
if (n%i==)
{
ans2+=(qword)i*phi(n/i);
ans2+=(qword)(n/i)*phi(i);
}
}
if (l*l==n)
{
ans2+=(qword)(n/l)*phi(l);
}
printf(LL"\n",ans2);
return ;
}
bzoj 2705: [SDOI2012]Longge的问题 歐拉函數的更多相关文章
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
- Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1959 Solved: 1229[Submit][ ...
- bzoj 2705 [SDOI2012]Longge的问题——欧拉函数大水题
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 撕逼题.不就是枚举gcd==d,求和phi[ n/d ]么. 然后预处理sqrt (n ...
- BZOJ 2705: [SDOI2012]Longge的问题
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2554 Solved: 1566[Submit][ ...
- BZOJ 2705: [SDOI2012]Longge的问题 GCD
2705: [SDOI2012]Longge的问题 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...
- BZOJ 2705: [SDOI2012]Longge的问题( 数论 )
T了一版....是因为我找质因数的姿势不对... 考虑n的每个因数对答案的贡献. 答案就是 ∑ d * phi(n / d) (d | n) 直接枚举n的因数然后求phi就行了. 但是我们可以做的更好 ...
- [bzoj]2705: [SDOI2012]Longge的问题[数论][数学][欧拉函数][gcd]
[bzoj]P2705 OR [luogu]P2303 Longge的问题 Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需 ...
- BZOJ 2705 [SDOI2012]Longge的问题(欧拉函数)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2705 [题目大意] 求出∑gcd(i,N)(1<=i<=N) [题解] $ ...
- BZOJ.2705.[SDOI2012]Longge的问题(莫比乌斯反演 欧拉函数)
题目链接 \(Description\) 求\[\sum_{i=1}^n\gcd(i,n)\] \(Solution\) \[ \begin{aligned} \sum_{i=1}^n\gcd(i,n ...
随机推荐
- Java的finally理解
1.为什么要用finally 先看一个没有finally的异常处理try-catch语句: 如果count为要使用到的资源,而且用完要求释放此资源.那么我们能够把释放资源的语句放到try-catch后 ...
- 将Java应用注册为后台服务
项目中有一个java应用程序,交付后用户要求要把这个程序做成后台服务程序,即:系统启动后该程序可以自动启动,并且在前台不要出现运行窗口,维护人员只要在“服务管理”(Windows)中选择启动或停止即可 ...
- "ORA-00942: 表或视图不存在 "的原因和解决方法
"ORA-00942: 表或视图不存在 "的原因和解决方法 采用Oracle数据库,使用Powerdesigner设计,生成Sql文件导入后查询出现“ORA-00942: 表或 ...
- awk用法举例
awk文本分割输出工具(按列输出工具) awk [options] ' PATTERN { action } ' file1, file2, ... 内置变量: FS:field separator, ...
- oracle如何获取上个月的月份
--转载 这个要用到add_months()函数 参数 负数 代表 往前 正数 代表 往后.select to_char(add_months(trunc(sysdate),-1),'yyyymm ...
- HDU-1009(简单贪心)
FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...
- Java Map集合按照key和value排序之法
一.理论基点 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black-Tre ...
- MyBatis的学习总结四:实现关联表查询【参考】
一.一对一的表关联查询(edi_test_task 和 edi_task_detail) 例子:一条任务明细对一条任务记录 对应的sql的映射xml文件如下: <?xml version=& ...
- UITextField 对输入金额的约束
[2016/1/18更新] -- 五个人辛辛苦苦干了一年的项目终于上线了,今天有空看了一下正则表达式教程,然后开始rebuild之前的种种对字符串的约束,首先就从这个金额输入框开始吧,修改后的代码如下 ...
- 设置lable内容不上下居中
转载自:http://dong-zsh.github.io/2015/10/14/%E8%AE%BE%E7%BD%AElable%E5%86%85%E5%AE%B9%E4%B8%8D%E4%B8%8A ...