LightOj 1336 Sigma Function(打表找规律)

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

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 nhave 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


解题思路:

题目大意:

求1—N中,有多少数的 σ(a) 值是偶数。

算法思想:

由暴力打表可知:

只要是2^x,x^2,2*x^2...只有这种数没有出现过。所以,我们直接去重即可。
但是这些直接去重我们会发现减去的这些值有重复的,所以我们要判断下。
①2^x和x^2,  当x为偶数时二者出现重复。
②2^x和2*x^2,当x为奇数时,二者出现重复。
所以直接用N值减去x^2和2*x^2的值就是我们要的结果。

暴力代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int MAXN = 1000005;
int vis[MAXN];
vector<int> prime;

void get_prime(){
    memset(vis,false,sizeof(vis));
    vis[1] = true;
    for(int i = 2; i <= 1000000; i++){
        int t = 1000000/i;
        for(int j = 2; j <= t; j++)
            vis[i*j] = true;
    }
    for(int i = 2; i <= 1000000; i++)
        if(!vis[i])
            prime.push_back(i);
}

ll power(ll x,int n){
    ll res = 1;
    while(n){
        if(n&1)
            res = res*x;
        x = x*x;
        n >>= 1;
    }
    return res;
}

int solve(ll n){
    while(n%2 == 0){
    	n/=2;
    }
    int len = prime.size();
    for(int i = 1; i < len && n; i++){
        int num = 0;
        if(n%prime[i] == 0){
            while(n%prime[i] == 0){
                n /= prime[i];
                num++;
            }
        }
        ll sum = (power(prime[i],num+1)-1);
        if((sum/(prime[i]-1))%2 == 0)
            return 1;
    }
    return 0;
}

int main(){
    get_prime();
    int T,t = 1;
    scanf("%d",&T);
    while(T--){
        ll n;
        scanf("%lld",&n);
        ll ans = 0;
        for(ll i = 3; i <= n; i++){
	        if(solve(i))
                ans++;
	    }
        printf("Case %d: %lld\n",t++,ans);
    }
    return 0;
}

AC代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main(){
    int T,t = 1;
    scanf("%d",&T);
    while(T--){
        ll n;
        scanf("%lld",&n);
        ll ans1 = (ll)sqrt(n*1.0);
        ll ans2 = (ll)sqrt(n*1.0/2);
        printf("Case %d: %lld\n",t++,n-ans1-ans2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值