HDOJ 3501 Calculation 2
题目链接
分析:
要求的是小于$n$的和$n$不互质的数字之和...那么我们先求出和$n$互质的数字之和,然后减一减就好了...
$\sum _{i=1}^{n} i[gcd(i,n)==1]=\left \lfloor \frac{n\phi(n)}{2} \right \rfloor$
考虑$gcd(n,i)=1$,那么必然有$gcd(n,n-i)=1$,然后发现如果把$gcd(n,i)=1$和$gcd(n,n-i)=1$凑到一起会出现$n$,这样的有$\left \lfloor \frac{\phi(n)}{2} \right \rfloor$对...
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
//by NeighThorn
using namespace std; const int mod=1e9+7; int n,ans; inline int phi(int n){
int x=n,m=sqrt(n);
for(int i=2;i<=m;i++)
if(n%i==0){
x=1LL*x/i*(i-1)%mod;
while(n%i==0)
n/=i;
}
if(n>1) x=x/n*(n-1)%mod;
return x;
} signed main(void){
while(scanf("%d",&n)&&n){
ans=(1LL*n*(n-1)/2)%mod;
ans-=(1LL*phi(n)*n/2)%mod;
if(ans<0) ans+=mod;
printf("%d\n",ans);
}
return 0;
}
By NeighThorn
HDOJ 3501 Calculation 2的更多相关文章
- HDU 3501 Calculation 2(欧拉函数)
Calculation 2 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- HDU 3501 Calculation 2------欧拉函数变形
Calculation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU——T 3501 Calculation 2
http://acm.hdu.edu.cn/showproblem.php?pid=3501 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- hdoj 3501
Problem Description Given a positive integer N, your task is to calculate the sum of the positive in ...
- HDU 3501 Calculation 2 (欧拉函数)
题目链接 题意 : 求小于n的数中与n不互质的所有数字之和. 思路 : 欧拉函数求的是小于等于n的数中与n互质的数个数,这个题的话,先把所有的数字之和求出来,再减掉欧拉函数中所有质数之和(即为eula ...
- hdu 3501 Calculation 2 (欧拉函数)
题目 题意:求小于n并且 和n不互质的数的总和. 思路:求小于n并且与n互质的数的和为:n*phi[n]/2 . 若a和n互质,n-a必定也和n互质(a<n).也就是说num必定为偶数.其中互质 ...
- HDU 3501 Calculation 2
题目大意:求小于n的与n不互质的数的和. 题解:首先欧拉函数可以求出小于n的与n互质的数的个数,然后我们可以发现这样一个性质,当x与n互质时,n-x与n互质,那么所有小于n与n互质的数总是可以两两配对 ...
- HDU 3501 Calculation 2 ——Dirichlet积
[题目分析] 卷积太有趣了. 最终得出结论,互质数和为n*phi(n)/2即可. 计算(n*(n+1)/2-n-n*phi(n)/2)%md,用反正法即可证明. [代码] #include <c ...
- 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)
Description Given a positive integer N, your task is to calculate the sum of the positive integers l ...
随机推荐
- LINUX系统配置相关
修改系统引导文件 grub.cfg的文件位置 /boot/grub/grub.cfg set default="4" 默认windows是在第四个选项 set timeout ...
- [转]使用Gradle管理你的Android Studio工程
本文转自:http://www.flysnow.org/2015/03/30/manage-your-android-project-with-gradle.html Gradle简介 Gradle ...
- jqgrid-parmNames和jsonReader的使用,以及json的返回格式(转)
prmNames : { page:"page", // 表示请求页码的参数名称 rows:"rows", // 表示请求行数的参数名称 sort: ...
- Kotlin操作符重载:把标准操作加入到任何类中(KAD 17)
作者:Antonio Leiva 时间:Mar 21, 2017 原文链接:https://antonioleiva.com/operator-overload-kotlin/ 就像其他每种语言一样, ...
- NO8——排序
//sort #include<algorithm> bool cmp(const int a,const int b) { return a>b;//降序排列 } //qsort ...
- Week 1 Team Homework #3 from Z.XML-软件工程在北航
任务名称:软件工程在北航 任务要求:要求我们采访往届师兄师姐,收集他们对于软件工程这门课程的反馈.具体作业链接http://www.cnblogs.com/jiel/p/3311403.html 任务 ...
- 使用ListOperations操作redis
使用ListOperations对象操作redis list: 方法 c参数 s说明 List<V> range(K key, long start, long end); K key ...
- Elasticsearch 监控和部署
Elasticsearch: ! [ https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_cluster_health. ...
- Source Tree基础教程2
1.分支 项目——分支——推送 新分支要重新拉取项目后才可以看见 项目——拉取 2合并分支代码 将其他分支代码合并到当前分支——提交
- 正则表达式之旅_sed_awk
谈谈正则表达式这个东西: 我想作为一个程序员,正则表达式大家绝对不陌生. 正则表达式好像一个有限则动机.主要作用是匹配,但是同时因为这个功能,我们可以扩展很多其他用法 像很多语言都引人了正则表达式:j ...