LightOJ1336 Sigma Function(约数和为奇数的个数)

来源:互联网 发布:手机壳效果图软件 编辑:程序博客网 时间:2024/05/19 00:08

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中,有多少数的 σ(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;    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;  }  

 

0 0
原创粉丝点击