不互质的不好找,但互质的好找
还记得更相减损术吗 gcd(a,b) = gcd(a,a-b),如果a,b互质,a,a-b也互质,(b,a-b)是一组与a互质且相加为a,有多少组呢?
phi(a)/2组
import java.io.*;
import java.util.*;
public class Main {
public static long power(long x,long n) {
long ans = 1;
final int Mod = 1000000007;
for ( ; n!=0; n>>=1,x = (x*x)%Mod) {
if (n%2 == 1) ans = (ans*x)%Mod;
}
return ans;
}
public static void main(String args[]) throws Exception {
final long Mod = 1000000007;
Scanner cin=new Scanner(System.in);
long inv2 = power(2L,Mod-2L);
while(true) {
long n = cin.nextLong();
if (n == 0) break;
if (n == 1) {
System.out.println(0);
continue;
}
long phi = n,a = n;
for (long i = 2; i*i <= a; i++)
if (a%i == 0) {
phi = phi/i*(i-1);
while (a%i == 0) a /= i;
}
if (a > 1) phi = phi/a*(a-1);
System.out.println(((n*(n+1L)/2-n)%Mod-n*phi%Mod*inv2%Mod+Mod)%Mod);
}
}
}
Problem Description
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.
Input
For 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.
Output
For each test case, you should print the sum module 1000000007 in a line.
Sample Input
3 4 0
Sample Output
0 2