Light OJ 1336 Sigma Function(因子和)

来源:互联网 发布:画画的软件 编辑:程序博客网 时间:2024/05/19 00:54

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 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 (n:1~1012)中,因子和为偶数的有几个。
题解:My New Blog

因子和 Sum=(p1^0+p1^1….p1^e1)*(p2^0+p2^1…p2^e2)……(pn^0+…pn^en); 

=

clip_image002

 

 

(p1^0+p1^1….p1^e1),(p2^0+p2^1…p2^e2),……(pn^0+…pn^en)中只要有一个是偶数,因子和sum就为偶数。所以只要找到一个是偶数就可以了。 

若pi为偶数,则pi^x(x>0)为偶数,而pi^0=1(1+偶+偶….为奇数)。So,(pi^0+pi^1+…pi^ei)为奇数。所以pi只能是奇数,才能使(pi^0+pi^1+…pi^ei)为偶数。

再看pi^x (若x为奇数,pi^x为奇数(奇*奇*…为奇)),So,(pi^0+pi^1+…pi^ei)为偶数(1+奇+奇…)

所以,对m素因子分解,只要存在一个pi,ei都为奇数的pi^ei,就能使sum为偶数。

然而这么做必定TLE(-。-;)

再想想,1~n中 有多少个的素因子分解中存在pi^ei(奇^奇)。

3^(2k-1)  *  (1,2,3,4..n/3)          (k>1&&3^(2k-1)<=n)

5^(2k-1)  *  (1,2,3,4…n/5)  

….

prime[i]^(2k-1)  *  (1,2,3,…n/prime[i])  能包含所有的解,然而还有好多重复的解。看着有点像容斥定理,但多了个条件。(prime[i]^1,prime[i]^3…容斥有问题)。想了好久,感觉这题好难,不会了。。

不会咋办,换个思路呗!

 

不过之前还是忍不住暴力了一下 果断TLE。

[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #include<cstdio>  
  2. #include<iostream>  
  3. #include<cstring>  
  4. using namespace std;  
  5. const int N=1e6+5;  
  6.   
  7. bool vis[N];  
  8. int prime[N],cnt;  
  9. void is_prime()  
  10. {  
  11.     cnt=0;  
  12.     memset(vis,0,sizeof(vis));  
  13.     for(int i=2;i<N;i++)  
  14.     {  
  15.         if(!vis[i])  
  16.         {  
  17.             prime[cnt++]=i;  
  18.             for(int j=i+i;j<N;j+=i)  
  19.                 vis[j]=1;  
  20.         }  
  21.     }  
  22. }  
  23.   
  24. bool is_even(long long n)  
  25. {  
  26.     for(int i=0;i<cnt&&prime[i]*prime[i]<=n;i++)  
  27.     {  
  28.         int count=0;  
  29.         if(n%prime[i]==0)  
  30.         {  
  31.             while(n%prime[i]==0)  
  32.             {  
  33.                 n/=prime[i];  
  34.                 count++;  
  35.             }  
  36.             if(prime[i]&1)  
  37.             {  
  38.                 if(count&1)  
  39.                     return true;  
  40.             }  
  41.         }  
  42.     }  
  43.     if(n>1&&(n&1))  
  44.         return true;  
  45.     return false;  
  46. }  
  47.   
  48. int main()  
  49. {  
  50.     int t;  
  51.     cin>>t;  
  52.     is_prime();  
  53.     for(int kase=1;kase<=t;kase++)  
  54.     {  
  55.         long long n;  
  56.         cin>>n;  
  57.         long long count=0;  
  58.         for(long long i=1;i<=n;i++)  
  59.         {  
  60.             if(is_even(i))  
  61.                 cout<<i<<endl;  
  62.                 count++;  
  63.         }  
  64.         printf(”Case %d: %d\n”,kase,count);  
  65.     }  
  66. }  

#

反过来想,什么时候因子和是奇数呢?
由前面的分析(只要存在一个pi,ei都为奇数的pi^ei,就能使sum为偶数),素因子分解后,全为奇^偶,偶^偶,偶^奇,因子和就是奇数。2是唯一一个偶素数。(特别的就得拿出来分开考虑。。)

((数^(偶/2)*(数^(偶/2))….)^2,这不是平方数吗!(数指的是奇数||偶数) (包含了数^偶

所以这些数是 平方数 || 2*平方数 。(想不通的可以拿奇^偶,偶^偶,偶^奇组合,发现全被平方数 || 2*平方数 包含了)



 

 



[cpp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. #include<cmath>  
  3. #include<cstdio>  
  4. using namespace std;  
  5. typedef long long LL;  
  6.   
  7. int main()  
  8. {  
  9.     int t;  
  10.     scanf(”%d”,&t);  
  11.     for(int kase=1;kase<=t;kase++)  
  12.     {  
  13.         LL n;  
  14.         scanf(”%lld”,&n);  
  15.         LL num1=(LL)sqrt((double)n);  
  16.         LL num2=(LL)sqrt((double)n/2.0);  
  17.         printf(”Case %d: %lld\n”,kase,n-num1-num2);  
  18.     }  
  19.     return 0;  
  20. }