[HDU3501] Calculation 2 (欧拉函数)

该博客介绍了如何计算与给定正整数N互质的正整数之和。当两个数互质时,它们的欧拉函数值可以被用于找到一组与原始数互质且和为原始数的数对。题目要求求解小于N且与N不互质的正整数之和,对于每个测试用例,需要对结果取模1000000007输出。
摘要由CSDN通过智能技术生成

题目 

不互质的不好找,但互质的好找

还记得更相减损术吗 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值