Given a positive integer N, your task is to calculate the sum of the positive integers less than N which are not coprime to N. A is said to be coprime to B if A, B share no common positive divisors except 1.

InputFor each test case, there is a line containing a positive integer N(1 ≤ N ≤ 1000000000). A line containing a single 0 follows the last test case.OutputFor each test case, you should print the sum module 1000000007 in a line.Sample Input

3
4
0

Sample Output

0
2 如果gcd(n,i)==1,gcd(n,n-i)==1
证:i=1(mod n)
  -i=-1(mod n)
  n-i=-1+n (mod n)
  n-i=1 (mod n)
通过欧拉函数一个数n中存在phi(n)个与之互质的数,因为i+(n-i)为n也就是有n对。
则与n互质数目之和为res=phi(n)/2*n,
ans=(n-1)*n/2-res。
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define N 1000010
#define maxn 1000010
#define mod 1000000007
using namespace std;
typedef long long ll;
int p[N];
int prime[N];
int pn=0;
bool vis[N];
int main()
{
for (int i = 2; i < N; i++) {
if (vis[i]) continue;
prime[pn++] = i;
for (int j = i; j < N; j += i)
vis[j] = 1;
}
ll n;
while(~scanf("%lld",&n),n)
{
ll r=n;
ll phi=n;
for(int i=0;prime[i]*prime[i]<=r;i++)
{
ll tem=0;
while(r%prime[i]==0)
{
r/=prime[i];
tem++;
}
if(tem)
phi=phi-phi/prime[i];
}
if(r!=1)
phi-=phi/r;
ll rem=n*phi/2;
ll ans=n*(n-1)/2;
printf("%lld\n",(ans-rem)%mod);
}
}

  

hdu_3501_Calculation 2的更多相关文章

随机推荐

  1. Magnum基本介绍

    Magnum is an OpenStack API service developed by the OpenStack Containers Team making container orche ...

  2. mysql中操作符LIKE与通配符%的使用

    mysql中通配符%用来通配其他字符,操作符LIKE用来查询字段中存在相同的字符 SELECT t.userId,t.cellphone,t.idNo,t.* FROM t_person t WHER ...

  3. Java对于成对括号的提取

    在工作的项目当中,经运营人员的反馈,发现提供服务的指定属性字段的值为空,导致搜索引擎无法正常搜索到正确的结果. 原始的字符串提取程序为: // 只取对应符号分割的第一部分name.split(&quo ...

  4. 保护REST API/Web服务的最佳实践

    在设计REST API或服务时,是否存在处理安全性(身份验证,授权,身份管理)的最佳实践? 在构建SOAP API时,您可以使用WS-Security作为指导,有关该主题的文献很多.我发现了有关保护R ...

  5. [转].NET Core配置文件加载与DI注入配置数据

    本文转自:http://www.cnblogs.com/skig/p/6079187.html .NET Core配置文件 在以前.NET中配置文件都是以App.config / Web.config ...

  6. SVN仓库连同版本信息迁移新服务器的步骤

    SVN仓库连同版本信息迁移新服务器的步骤 步骤一:导出(1)链接原服务器,找到SVN Server安装路径下的bin文件,并复制文件路径,如 C:\Program File\SVN Server\bi ...

  7. C# 操作 Excel(.xls和.xlsx)文件

    C#创建Excel(.xls和.xlsx)文件的三种方法 .NET 使用NPOI导入导出标准Excel C# 使用NPOI 实现Excel的简单导入导出 NET使用NPOI组件将数据导出Excel-通 ...

  8. Weblogic中配置Active Directory Authentication Provider

    其要点或者容易出错的关键点是:(<>及其中说明代表需要替换的内容)         Host: ads.yourdomain.com         Host填AD服务器的域名或IP    ...

  9. ACM-单调队列

    对于单调队列的基本概念可以去看百科里的相关介绍:http://baike.baidu.com/view/3771451.htm 这里挑一些重点. 作用: 不断地向缓存数组里读入元素,也不时地去掉最老的 ...

  10. ORACLE:毫秒与日期的相互转换,获取某天的信息

    毫秒转换为日期 SELECT TO_CHAR(1406538765000 / (1000 * 60 * 60 * 24) + TO_DATE('1970-01-01 08:00:00', 'YYYY- ...