一、题目描述
Sigma function is an interesting function in Number Theory. It is denoted by the Greek letter Sigma (σ). This function actually denotes the sum of all divisors of a number. For example σ(24) = 1+2+3+4+6+8+12+24=60. Sigma of small numbers is easy to find but for large numbers it is very difficult to find in a straight forward way. But mathematicians have discovered a formula to find sigma. If the prime power decomposition of an integer is
Then we can write,
For some n the value of σ(n) is odd and for others it is even. Given a value n, you will have to find how many integers from 1 to n have even value of σ.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 1012).
Output
For each case, print the case number and the result.
Sample Input
4
3
10
100
1000
Sample Output
Case 1: 1
Case 2: 5
Case 3: 83
Case 4: 947
二、算法分析说明
已知:奇数 × 奇数 = 奇数,偶数 × 奇数 = 偶数,偶数 × 偶数 = 偶数。
要求 [1, n] 内 S 是偶数的数 X 的个数,但 S 是偶数的条件不好确定。不过,由于任意个奇数相乘仍然是奇数,所以可以利用 “正难则反” 思想,先筛选出约数和 S 为奇数的数的个数,再用总数减去这个数。
当一个数 X 含有质因子 2 时,2 的正整数次方都是偶数,这些偶数相加也是偶数,再加 1 就是奇数。
当一个数含有其它质因子(都是奇数)时,奇数的正整数次方都是奇数。偶数个奇数相加是偶数,奇数个奇数相加是奇数。所以仅这个数含有该奇质数的偶数次方时,约数和的表达式中对应的乘数才是奇数。
如果一个数的唯一分解式中仅含有奇质数的偶数次方的因子,那么这个数是平方数。在此条件下,如果在该数的唯一分解式中添加 2 的任意次方作为因子,那么它的约数和依然是奇数。归结起来,所有平方数和它们的两倍就是 [1, n] 范围内全部约数和为奇数的数。显然,集合 {n2} 和 {2n2} 是不交的。
所以最终答案是:n - sqrt(n) - sqrt(n / 2)。
三、AC 代码(0 ms)
#include<cstdio>
#include<cmath>
#pragma warning(disable:4996)
unsigned t; unsigned long long n;
int main() {
scanf("%u", &t);
for (unsigned i = 1; i <= t; ++i) {
scanf("%llu", &n);
printf("Case %u: %llu\n", i, n - (unsigned long long)sqrt(n) - (unsigned long long)sqrt(n / 2));
}
return 0;
}