Sigma Function(LightOJ-1336)
Problem 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
題意:t 組數據,每組給出 1 個數 n,求 1~n 中,所有數的約數和是偶數的數的個數
思路:
通過給的?
可以發現,式子中的每一項都是一個等比數列??的和
那么,
因此,對于式子?,只要其中的一項為偶數,那么??就一定為偶數,故而只要找到一項為偶數就可以
觀察式子可以發現:當??為偶數時,那么??一定為偶數,而?,那么由于 1+偶+偶+...+偶=奇,因此??一定是一個奇數,那么,只有當??為奇數時,?才可能為偶數
再看?,:
- 當??為偶數時,此時有偶數個奇數項,則:1+奇+奇+...+奇=奇,此時??仍為一個奇數
- 當 ?為奇數時,此時有奇數個奇數項,則:1+奇+奇+...+奇=偶,此時??才是一個偶數
因此,對 n 做素因子分解,只要存在一個??均為奇數,那么??就一定為偶數
然而這樣做會 TLE,因此還要進一步的推導:
我們可以考慮使用?總數-不滿足的數=滿足的數 來求解,也即求出所有??不均為奇數的數
當素因子分解后,有四種形式:奇^奇、偶^偶、奇^偶、偶^奇,根據前面的分析,只有當??均為奇數時,?才為偶數
那么,如果當素因子分解后,全是?偶^偶、奇^偶、偶^奇 的形式,因子和就一定為奇數
由于 2 是唯一的偶素數,我們將這個特殊的數拿出來分開考慮,根據唯一分解定理 ?
那么:
- 當??為偶數時,有 偶^偶、奇^偶 兩種情況,其能拆成某個數的平方,即:偶^偶=( 偶^(偶/2) )^2、奇^偶=( 奇^(偶/2) )^2,那么 偶^奇、奇^偶 則兩種情況就被 n 以內的平方數 給包含了,即有??種情況
- 當??為奇數時,有 偶^奇 一種情況,由于其一定能拆成 2*x^2 的形式,那么?偶^奇 的情況就被?n 以內的 2*平方數?給包含了,即有??種情況
故最后結果為:
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define LL long long const int MOD=10007; const int N=1000000+5; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std; int main(){int t;scanf("%d",&t);int Case=1;while(t--){LL n;scanf("%lld",&n);LL temp1=(LL)sqrt(n);LL temp2=(LL)sqrt(n/2);printf("Case %d: %lld\n",Case++,n-temp1-temp2);}return 0; }總結
以上是生活随笔為你收集整理的Sigma Function(LightOJ-1336)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: How Many Pieces of L
- 下一篇: 2-SAT 问题(洛谷-P4782)