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;
}