Sigma Function LightOJ - 1336 数论经典推理

题目:

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 中,所有数的约数和是偶数的数的个数。

有一个理论:

偶数 * 偶数 = 偶数;

奇数 * 偶数 = 偶数;  

奇数 * 奇数 = 奇数;

整数唯一分解定理:x = p1^a1  *  p2^a2  *……* pk^ak

因数和公式 σ(x)= (1+p1+p1^2+……+p1^a1) * (1+p2+p2^2+……+p2^a2) *……* (1+pk+pk^2+……+pk^ak)         (说明一下:题目中的式子因数和公式就是有由当前这个式子推导出来的)

我们的目的就是要使(1+pi+pi^2+……+pi^ai)这个东西都是奇数,这样就可以确定这个因数和为奇数。

现在考虑两种情况(因为有一个质因子是2,所以要单独考虑它)

⑴质因子为2的时候,当pi=2的情况这部分等比数列求和一定是奇数。

⑵pi是其他,再观察这个式子(1+pi+pi^2+……+pi^ai),我们发现这式子里面每一项都是奇数,所以只有当ai是偶数的时候,(1+pi+pi^2+……+pi^ai)这个值才是奇数,所以我们回到整数唯一分解定理看x = p1^a1  *  p2^a2  *……* pk^ak,我们只需要把ai的值都变成偶数即可,可以变成x = p1^(a1'*2)  *  p2^(a2'*2)  *……* pk^(ak'*2) = (p1^a1'  *  p2^a2'  *……* pk^ak')^2。

如果没有2这个素因子一定是完全平方数,如果有2,相当于一个完全平方数*2的几次方,所以它要么也是一个完全平方数(2的偶数次方)或者2*一个完全平方数(2的奇数次方,提出一个2其他构成完全平方数)

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int t,k=0;
    cin>>t;
    while(t--){
        ll n,ans=0;
        cin>>n;
        for(ll i=1; i*i<=n; i++){
            ans++;
            if(2*i*i<=n)ans++;
        }
        printf("Case %d: %lld\n",++k,n-ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值