Sum it up (欧拉函数)

Sum it up

Problem Description

Given a integer n (0 < n < 1e6), find the sum of x, which x meet the following conditions: gcd(x, n)!=1 && 0<x<=n. BTW, gcd(m, n) means greatest common divisor between m and n.

Input

The first line of the input file contains T (T<500), indicating the number of test cases.

There is only one number in each test cases: n, the meaning of which has been described above.

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the sum. (without quotation)

Sample Input

2

3

10

Sample Output

Case #1: 3

Case #2: 35

//题意:给你一个数n,让你找出在[1---n]之间与n不互质的数的和。

//思路:欧拉函数先求出与n互质的数的个数,再根据公式求得与n互质的数的和ans=eulor(n)*n/2,再用总和减去就行了sum=(n+1)*n/2-ans;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll oula(int x)
{
	int ans = x;
	for(int i = 2; i <= sqrt(x); i++)
	{
		if(x % i == 0)
		{
			ans = ans * (i - 1)/i;
			while(x % i == 0)
			{
				x /= i;
			}	
		}
		
	}
	if(x > 1)
		ans = ans * (x - 1)/x;
	return ans;
}
int main()
{
	int T, n, kase = 0;	
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d", &n);
		ll sum=(ll)n*(n+1)/2;
		ll ans=oula(n)*n/2;
		printf("Case #%d: %lld\n",++kase,sum-ans);
	}
	return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值