HDU 3501 Calculation 2 (Euler function application)

Source: Internet
Author: User


Calculation 2 Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total Submission (s): 2989 Accepted Submission (s): 1234


problem DescriptionGiven A positive integer n, your task is to calculate the sum of the positive integers less than N which was not copri Me to N. A is said to being coprime to B if A, B share no common positive divisors except 1. Inputfor each test case, the 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 of module 1000000007 in a line. Sample Input
340
 Sample Output
02
 AuthorGtmac Sourceacm-icpc multi-university Training Contest (7)--host by hits

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3501

The main idea: to find less than N and n non-coprime number of

Title Analysis: Considering the number of n coprime and is less than N and very good, and then subtract with the sum, less than n and n coprime number and equal to N*phi[n]/2,phi[n] is n corresponding Euler function value, proved as follows:
Set GCD (n, i) = = 1, then have GCD (n, n-i) = = 1
This can be disproved: suppose there is a k! = 1,GCD (n, n-i) = = k, then there is n%k = = 0 and (n-i)% K = = 0 that is (n k-i% k)% K = = 0, get (i% k)% K = = 0
So I must be a multiple of k, so GCD (n, i) = = k, which conflicts with GCD (n, i) = = 1, so for GCD (n, i) = = 1, there is GCD (n, n-i) = = 1, so the two logarithm with n coprime is n and the total number of n coprime is phi[n], note here does not appear n = = 2*i case because 2,phi[i] is even
So the sum of the number of n coprime is less than n and equal to N*phi[n]/2, so the final answer is (n * (n-1)/2-n*phi[n]/2)% MOD, which asks the complexity of the single Phi to be sqrt ((n)

#include <cstdio> #include <cmath> #define LL long longint Const MOD = 1e9 + 7;int phi (int x) {    int res = x;< c1/>for (int i = 2; I * i <= x; i++)    {        if (x% i = = 0)         {            res = res/i * (i-1);            while (x% i = = 0)                 x/= i;        }    }    if (x > 1)         res = res/x * (x-1);    return res;} int main () {    int n;    while (scanf ("%d", &n)! = EOF && N)    {        ll sum = (LL) n * (n-1)/2;        ll copsum = (LL) n * PHI (n)/2;        ll ans = sum-copsum;        printf ("%i64d\n", ans% MOD);}    }





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3501 Calculation 2 (Euler function application)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.