LightOj 1336 Sigma Function----数论之因素之和

Time limit   2000 ms

Memory limit   32768 kB

OS   Linux

Source   Problem Setter: Shahriar Manzoor

              Special Thanks: Jane Alam Jan (Solution, Dataset)

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

题意:这道题是求1~n个数中因素之和为偶数的个数。

题目中的式子:

还有一种写法:

就简单的根据题目例子来说,可以知道\sigma1(N)为偶数的个数有很多,所以我们可以先计算\sigma1(N)为奇数的个数。上述公式中p为质因子,a为质因子的指数。因为除了2以外所有的质因子都是奇数,所以首先我们先讨论p为2的情况,如果p为2,那么不管a是奇数还是偶数,p^a都是偶数,再加上1就是奇数即(1+p+……+p^a)为奇数;当p不为2时,可以知道只有a为偶数的情况,p^a才是偶数,再加上1就是奇数了。而奇数*奇数=奇数,所以只要判断a为偶数就可以知道奇数个数。假设x,显然2^x为偶数,2*2^x页为偶数。所以奇数的个数=sqrt(n)+sqrt(n/2);减去奇数就可以得到最终答案。

#include <iostream>
#include <math.h>
#include <stdio.h>
#define ll long long
using namespace std;

int main()
{
    int t;
    ll n;
    scanf("%d",&t);
    int cont=0;
    while(t--)
    {
        scanf("%lld",&n);
        ll te=n;

        te-=(int)sqrt(n)+(int)sqrt(n/2);

        printf("Case %d: %lld\n",++cont,te);
    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_41233888/article/details/81415285
今日推荐