LightOJ - 1336 Sigma Function (n以內約數和爲奇數(偶數)的數的個數)

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



這裏n很大不能直接暴力的

關於sigma(n)的式子有個想法是這樣的:sigma(n)=(1+q1+...+q1^e1)*(1+q2+...+q2^e2)*....*(1+qn+...+qn^en)

很顯然當q[i]不爲2的時候,當e[i]爲奇數時括號內的和爲偶數,e[i]爲偶數時括號內的和爲奇數,

當q[i]==2時,括號內的和必爲奇數;

這個題到這一步我都想到了,但接下來的我就GG了....

這個題直接求偶數的個數的話就是直接求存在q[i]不爲2且e[i]爲奇數的數的個數,但這個我想了很久覺得好像沒什麼辦法可以求出來(可能是因爲我很菜吧)

那麼可以反着來:求sigma爲奇數的個數,然後減一減

當sigma爲奇數的時候,必有:q[i]不爲2的項的e[i]全部爲偶數

於是sigma(s)爲奇數的時候,s可以以兩種方法得到:s=a^2,s=2*b^2;

(不管a原來的sigma是怎麼樣的,a^2一定是“所有的e[i]都爲偶數,使得任意括號內的和都爲奇數,從而a^2爲奇數”)

(b也與a一樣,但是a的2的次數一定也是偶數,然而sigma爲奇數的數中,2的次數是可奇可偶的,所以有乘以2)

然後s還要滿足s<=n,

於是a的個數,b的個數分別爲sqrt(n),sqrt(n/2),所以sigma爲奇數的數的個數爲sqrt(n)+sqrt(n/2)....然後減一減

(嗚哇~做過一遍的題還不會做,感覺好傷)

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=2e5+3;
LL n;


int main()
{
    int T;
    scanf("%d",&T);
    int cas=0;
    while(T--)
    {
        scanf("%lld",&n);
        printf("Case %d: %lld\n",++cas,n-(LL)sqrt(n)-(LL)sqrt(n/2));
    }
    return 0;
}
大概就是這樣,如有錯誤,還望指正

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章