2013-08-11 21:59:51Morris

[UVA] 11395 - Sigma Function

Problem A
Sigma Function

Input: StandardInput

Output: StandardOutput

 

Sigma function is an interesting function in NumberTheory. It is denoted by the Greek letter Sigma (σ). This functionactually 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 largenumbers it is very difficult to find in a straight forward way. Butmathematicians have discovered a formula to find sigma. If the prime powerdecomposition of an integer , then

For some n the value of σ(n) is odd and forothers it is even. Given a value n, you will have to find how many integersfrom 1 to n have even value of σ.

 

 

Input

The input file contains at most 100 lines of inputs.

 

Each line contains an integer N(0<N<1000000000001).

 

Input is terminated by a line containing a singlezero. This line should not be processed.

 

Output

For each line of input produce one line of output.This line denotes how many numbers between 1 and N (inclusive) has even valueof function σ.

 

Sample Input                                                      Output for Sample Input

3

10

1000

0

1

5

947


Problemsetter: ShahriarManzoor

題目描述:

問 1~n 有多少個數,代入那個 sigma function 出來會是偶數。

題目解法:

問偶數,那打算用扣奇數的方式。

特別考慮一下,唯一個偶質數 2,不管次方為何,單一項加總都是奇數。

再考慮其他質數,如果總乘積要是奇數,那麼必須該質數的次方也是偶數。

那麼會得到該數一定是完全平方數(再替除 2 後)。

因此考慮 2 的次方數後,計算可搭配的奇完全平方數個數。


#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;

int main() {
    long long n;
    int i, j;
    while(scanf("%lld", &n) == 1 && n) {
        long long ret = n;
        long long t2 = 1;
        for(t2 = 1; t2 <= n; t2 *= 2)
            ret -= ((long long)(sqrt(n/t2)+1))/2;
        printf("%lld\n", ret);
    }
    return 0;
}