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. vue脚手架 && 实例

    什么是vue脚手架? 即可以快速构建vue项目的工具,通过它,我们可以将vue项目所需要的文件等安装完成. 为什么要是用vue脚手架,优点何在? 因为创建一个vue项目需要很多的依赖,文件的设置等,而 ...

  2. Spring Boot使用mongo的GridFS模块

    1. GridFS简介 GridFS是Mongo的一个子模块,使用GridFS可以基于MongoDB来持久存储文件.并且支持分布式应用(文件分布存储和读取).作为MongoDB中二进制数据存储在数据库 ...

  3. POJ 3259——Wormholes——————【最短路、SPFA、判负环】

    Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

  4. webpack.config.js====图片处理

    1. 安装依赖: cnpm install --save-dev url-loader image-webpack-loader html-loader 2. webpack.config.js规则的 ...

  5. Hibernate课程 初探一对多映射3-2 单向多对一的配置

    1 多方实体类中加入,一方类和getset方法 //多方定义一个一方的引用 private Grade grade; public Grade getGrade() { return grade; } ...

  6. 解决 Maven 项目中找不到 jdk 的 tools.jar 文件的办法(多数情况下适用)

    <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifact ...

  7. laravel安装时openssl_encrypt() 的问题?Call to undefined function openssl_decrypt()

    解决方案: 如果通过上面的步骤还是不能解决参考如下: 1.从php安装根目录中拷贝 libeay32.dll 和 ssleay32.dll 然后 覆盖掉apache/bin 下的对应文件(注意需要将h ...

  8. input type="file"获取文件名方法

    文件上传比较丑,样式调整时会有一个获取文件名,或者包含文件路径的文件名的方法 html代码 <div class="file-box"> <form id=&qu ...

  9. [转c#]记录程序耗时的方法

    多时候需要输出程序耗时,然后记录下来, 总是在程序执行开始记录当前时间点,在结尾记录结束时间点,然后两个时间相减,   那么有没有其他稍微像样点的方法呢?  告诉你,有滴 ; )   这个方法主体就是 ...

  10. adb使用wifi无线连接调试Android设备

    先上官方原文: Wireless usage adb is usually used over USB. However, it is also possible to use over Wi-Fi, ...