Sigma Function(LightOJ-1336)

Problem Description

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

题意:t 组数据,每组给出 1 个数 n,求 1~n 中,所有数的约数和是偶数的数的个数

思路:

通过给的 

可以发现,式子中的每一项都是一个等比数列 p_i^0+p_i^1+p_i^2+...+p_i^{e_i} 的和

那么,\sigma (n)=(p_1^0+p_1^1+p_1^2+...+p_1^{e_1})*(p_2^0+p_2^1+p_2^2+...+p_2^{e_2})*...*(p_k^0+p_k^1+p_k^2+...+p_k^{e_k})

因此,对于式子 \sigma (n)=(p_1^0+p_1^1+p_1^2+...+p_1^{e_1})*(p_2^0+p_2^1+p_2^2+...+p_2^{e_2})*...*(p_k^0+p_k^1+p_k^2+...+p_k^{e_k}),只要其中的一项为偶数,那么 \sigma (n) 就一定为偶数,故而只要找到一项为偶数就可以

观察式子可以发现:当 p_i 为偶数时,那么 p_i^x(x>0) 一定为偶数,而 p_i^0=1,那么由于 1+偶+偶+...+偶=奇,因此 p_i^0+p_i^1+p_i^2+...+p_i^{e_i} 一定是一个奇数,那么,只有当 p_i 为奇数时,p_i^0+p_i^1+p_i^2+...+p_i^{e_i} 才可能为偶数

再看 p_i^{e_i}(x>0)p_i^0=1

  • 当 e_i 为偶数时,此时有偶数个奇数项,则:1+奇+奇+...+奇=奇,此时 p_i^0+p_i^1+p_i^2+...+p_i^{e_i} 仍为一个奇数
  • e_i 为奇数时,此时有奇数个奇数项,则:1+奇+奇+...+奇=偶,此时 p_i^0+p_i^1+p_i^2+...+p_i^{e_i} 才是一个偶数

因此,对 n 做素因子分解,只要存在一个 p_i,e_i 均为奇数,那么 \sigma (n) 就一定为偶数

然而这样做会 TLE,因此还要进一步的推导:

我们可以考虑使用 总数-不满足的数=满足的数 来求解,也即求出所有 p_i,e_i 不均为奇数的数

当素因子分解后,有四种形式:奇^奇、偶^偶、奇^偶、偶^奇,根据前面的分析,只有当 p_i,e_i 均为奇数时,\sigma (n) 才为偶数

那么,如果当素因子分解后,全是 偶^偶、奇^偶、偶^奇 的形式,因子和就一定为奇数

由于 2 是唯一的偶素数,我们将这个特殊的数拿出来分开考虑,根据唯一分解定理  

那么:

  • 当 e_i 为偶数时,有 偶^偶、奇^偶 两种情况,其能拆成某个数的平方,即:偶^偶=( 偶^(偶/2) )^2、奇^偶=( 奇^(偶/2) )^2,那么 偶^奇、奇^偶 则两种情况就被 n 以内的平方数 给包含了,即有 \sqrt n 种情况
  • 当 e_i 为奇数时,有 偶^奇 一种情况,由于其一定能拆成 2*x^2 的形式,那么 偶^奇 的情况就被 n 以内的 2*平方数 给包含了,即有 \sqrt {\frac{n}2} 种情况

故最后结果为:n-\sqrt n-\sqrt {\frac{n}2}

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=10007;
const int N=1000000+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;
int main(){

    int t;
    scanf("%d",&t);

    int Case=1;
    while(t--){
        LL n;
        scanf("%lld",&n);
        LL temp1=(LL)sqrt(n);
        LL temp2=(LL)sqrt(n/2);
        printf("Case %d: %lld\n",Case++,n-temp1-temp2);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值