24h購物| | PChome| 登入
2013-08-11 21:59:51| 人氣2,387| 回應0 | 上一篇 | 下一篇

[UVA] 11395 - Sigma Function

推薦 0 收藏 0 轉貼0 訂閱站台

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

台長: Morris
人氣(2,387) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA] 795 - Sandorf's Cipher
此分類上一篇:[UVA][插頭DP] 11270 - Tiling Dominoes

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文