lightoj 1336 - Sigma Function(算数基本定理)

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

Output for Sample Input

4

3

10

100

1000

Case 1: 1

Case 2: 5

Case 3: 83

Case 4: 947



题意:让你求1~n中有多少个数的约数和是偶数。

这里我们利用了算数基本定理的另一条,N的约数和 = (1+p1+p1^2+p1^3+...+p1^a1)*(1+p2+p2^2+...+p2^a2)*...*(1+pn+pn^2+...+pn^an);

我们知道偶数乘偶数等于偶数,奇数乘奇数等于奇数,偶数乘奇数等于偶数,题目让求为偶数的数量,我们可以反过来求等于奇数的数量有多少,然后用n一减就得到了答案。

又因为质数中除了2,其他都是奇数,所以pi的所有次方都是奇数,那么要求奇数的数量,那么就得让每一个乘积项的值都为奇数,所以ai的值就一定是偶数。如果ai的值为偶数,就可以发现n就是一个完全平方数。

又因为不管2的幂次是多少,它的次方和全是偶数,再加上一个1,肯定为奇数,所以一个完全平方数的2倍的约数和也是奇数,当然一个完全平方数的4倍的约数和也是奇数,不过乘上4之后就变成了另一个完全平方数,所以就不用单独计算了。

所以1~n中约数和的数量就等于n - 1~n中完全平方数的个数 - 1~n中完全平方数的2倍小于n的个数(就相当于1~n/2中完全平方数的个数)


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

int main(void)
{
    int T;
    LL n;
    scanf("%d",&T);
    int cas = 1;
    while(T--)
    {
        scanf("%lld",&n);
        LL ans = n - (int)sqrt(n) - (int)sqrt(n/2);
        printf("Case %d: %lld\n",cas++,ans);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值