LightOJ-1336-Sigma Function-数论-思维

LightOJ-1336-Sigma Function-数论-思维

【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

>n=pe11pe22pekk> > n = p 1 e 1 ∗ p 2 e 2 ∗ ⋯ ∗ p k e k >

Then we can write,

>σ(n)=pe1+111p11pe2+121p21pek+1k1pk1> > σ ( n ) = p 1 e 1 + 1 − 1 p 1 − 1 ∗ p 2 e 2 + 1 − 1 p 2 − 1 ∗ ⋯ ∗ p k e k + 1 − 1 p k − 1 >

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 ≤ 10^12).

【Output】

 For each case, print the case number and the result.

【Examples】

Sample Input

4
3
10
100
1000

Sample Output

Case 1: 1
Case 2: 5
Case 3: 83
Case 4: 947

【Problem Description】

σ(n)为n的所有因子的和,给你一个n,问[1,n]中,σ(i)为偶数的个数有多少个。

【Solution】

数论
将题目给的公式看懂就比较简单了吧。
题目给的公式的每一项起始就是一个等比数列的和。即:

>σ(n)=(1+p1+p21++pe11)(1+p2+p22++pe22)(1+pk+p2k++pekk)> > σ ( n ) = ( 1 + p 1 + p 1 2 + ⋯ + p 1 e 1 ) ∗ ( 1 + p 2 + p 2 2 + ⋯ + p 2 e 2 ) ∗ ⋯ ∗ ( 1 + p k + p k 2 + ⋯ + p k e k ) >

则偶数的个数=n-奇数的个数
奇数的个数=小于n的平方数的个数。

1、要想σ(n)为奇数,则必须保证乘法的每一项都为奇数。
2、要保证每一项都为奇数,则必须保证(pi+pi^2+...+pi^ei)有偶数项,即ei为偶数。
3、特殊情况:pi为2时,ei无论何值,此项之和都为奇数。

对于每个ei都为偶数,则n必为平方数。
平方数*2<n,则个数加1

【Code】

/*
 * @Author: Simon 
 * @Date: 2018-09-06 13:37:22 
 * @Last Modified by: Simon
 * @Last Modified time: 2018-09-06 13:43:24
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 100005
int a[maxn];
Int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    for(int ca=1;ca<=t;ca++)
    {
        cout<<"Case "<<ca<<": ";
        int n;
        cin>>n;
        int sum=0;
        int i=1;
        while(i*i<=n)
        {
            sum++;
            if(i*i*2<=n) sum++;
            i++;
        }
        cout<<n-sum<<endl;
    }
    cin.get(),cin.get();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值