Hdoj 3501 "Euler function for numbers less than or equal to n sum of n coprime"

Source: Internet
Author: User
Calculation 2

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2548 Accepted Submission (s): 1064


Problem Description Given A positive integer N, your task is to calculate the sum of the positive integers less than N whi CH is not coprime to N. A is said to being coprime to B if A, B share no common positive divisors except 1.
Input for each test case, there was 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 title meaning: The sum of the number of n coprime, or N, of a number less than or equal to.   Euler function Extension: The number of less than or equal to N (n > 1), and the sum of n coprime euler[n] * n/2. Code:
Idea: An extension of the Euler function: a number less than or equal to N, and the sum of the number of n coprime: Euler (x) * X/2. (n>1)
#include <cstdio>
#include <cstring>
#include <cmath>
#define LL Long Long 
#define MOD 1000000007
using namespace std;
ll Euler (ll N)
{
	ll i;
	LL EU = n;
	for (i = 2; i*i <= n; i++)
	{
		if (n% i = = 0)
		{
			EU = EU * (i-1)/I;
			while (n% i = = 0)
			n/= i;
		}
	}
	if (n > 1) EU = EU * (n-1)/n;
	return EU;
}
int main ()
{
	LL n;
	while (scanf ("%lld", &n), N)
	{
		LL ans = n * (n+1)/2;
		Ans = Ans-euler (n) * N/2;
		printf ("%lld\n", (ans-n)%mod);
	}
	return 0;
}

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.