D - Sigma Function ( 因子和为奇数的特点 )
Discription
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因子和为偶数的数的个数。
思路:
数x的因子和 f(x)= (1+p1+p1^2+p1^3+...+p1^a1)*(1+p2+p2^2+...+p2^a2)*...*(1+pn+pn^2+...+pn^an);
因为偶数乘偶数还是偶数,奇数乘奇数还是奇数,奇数乘偶数是偶数,所有必须让每个括号内都是奇数,然后减去约数和为奇数的个数就是答案了。
1.当x有素因子2的时候,2所对应的括号内的和肯定是一个奇数,因为偶数加1一定是奇数。
2.除了2以外,所有的素数都是奇数,要使x得其他素因子对应的括号内的和为奇数,就必须保证x有偶数个该素因子,即ai必须为偶数。
3.满足上面两个条件的数,就是一个平方数,也就是说约数和为奇数的数x,它必定是一个平方数,当然这个平方数x乘上2也是满足2*x的约数和为奇数的。
所以只要减去用n减去sqrt(n)和sqrt(n/2)就是答案了。也可以找n以内的平方数的个数,以及2*平方数不超过n的数的所有个数和,用n减完之后就是答案。
可以这样理解, 对于一个平方数x,先开方得到y, 根据唯一分解定理,y这个数一定可以化成y = 的形式,这样再平方的话,所有指数乘二那么指数就全是偶数,这样x就符合2这个条件了, 再根据1这个条件括号里可以是2, 那么答案就是:平方数和平方数*2 的数因子和为奇数。
代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long ji=1,i,n,listt;
cin >> listt;
while ( listt-- ) {
cin >> n;
long long ans = n - (int)sqrt(n) - (int)sqrt(n/2);
cout << "Case " << ji++ << ": ";
cout << ans << endl;
}
return 0;
}