题目链接

分析:

要求的是小于$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的更多相关文章

  1. HDU 3501 Calculation 2(欧拉函数)

    Calculation 2 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  2. HDU 3501 Calculation 2------欧拉函数变形

    Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  3. HDU——T 3501 Calculation 2

    http://acm.hdu.edu.cn/showproblem.php?pid=3501 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  4. hdoj 3501

    Problem Description Given a positive integer N, your task is to calculate the sum of the positive in ...

  5. HDU 3501 Calculation 2 (欧拉函数)

    题目链接 题意 : 求小于n的数中与n不互质的所有数字之和. 思路 : 欧拉函数求的是小于等于n的数中与n互质的数个数,这个题的话,先把所有的数字之和求出来,再减掉欧拉函数中所有质数之和(即为eula ...

  6. hdu 3501 Calculation 2 (欧拉函数)

    题目 题意:求小于n并且 和n不互质的数的总和. 思路:求小于n并且与n互质的数的和为:n*phi[n]/2 . 若a和n互质,n-a必定也和n互质(a<n).也就是说num必定为偶数.其中互质 ...

  7. HDU 3501 Calculation 2

    题目大意:求小于n的与n不互质的数的和. 题解:首先欧拉函数可以求出小于n的与n互质的数的个数,然后我们可以发现这样一个性质,当x与n互质时,n-x与n互质,那么所有小于n与n互质的数总是可以两两配对 ...

  8. HDU 3501 Calculation 2 ——Dirichlet积

    [题目分析] 卷积太有趣了. 最终得出结论,互质数和为n*phi(n)/2即可. 计算(n*(n+1)/2-n-n*phi(n)/2)%md,用反正法即可证明. [代码] #include <c ...

  9. 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)

    Description Given a positive integer N, your task is to calculate the sum of the positive integers l ...

随机推荐

  1. ES6 export,import报错

    问题描述: 现有两个文件: profile.js const firstName = 'Michael'; const lastName = 'Jackson'; const year = 2018; ...

  2. Wordpress 从 MySQL 获取文章链接 permalinks

    SELECT wpp.post_title, wpp.guid, wpp.post_date, REPLACE( REPLACE( REPLACE( REPLACE( wpo.option_value ...

  3. 当我们在安装tensorflow时,我们在安装什么?- Intro to TF, Virtualenv, Docker, CUDA, cuDNN, NCCL, Bazel

    (Mainly quoted from its official website) Summary: 1. TensorFlow™ is an open source software library ...

  4. git instaweb 500 error

    在arch 系统中安装perl-cgi包. 在deiban中参考:https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A ...

  5. 对 a = [lambda : x for x in range(3)] 的理解

    上面的语句创建了一个列表 a ,其中有三个元素,每个元素都是一个 lambda 匿名函数. >>> a = [lambda : x for x in range(3)] >&g ...

  6. 一篇文章解决django中时区问题

    首先要明确的是,当在Django项目的setting.py文件中设置了USE_TZ=True时,我们给定的时间存储到数据库的时候都会变成UTC时间(使用auto_now_add和auto_now为Tr ...

  7. org.json.Json Object的put和append方法比较

    json.append(key,value) 会把 value 包装成一个数组 JSONObject append = new JSONObject().append("a", & ...

  8. Java作业09-异常

    6. 为如下代码加上异常处理 byte[] content = null; FileInputStream fis = new FileInputStream("testfis.txt&qu ...

  9. Concurrent.util中的一些类

    package com.bjsxt.height.concurrent019; import java.io.IOException; import java.util.Random; import ...

  10. QueryHelper插件类(hql)

    package cn.itcast.core.util; import java.util.ArrayList; import java.util.List; public class QueryHe ...