Sigma Function LightOJ - 1336 數論經典推理
題目:
Sigma function is an interesting function in Number Theory. It is denoted by the Greek letterSigma (σ). 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 somenthe value ofσ(n)is odd and for others it is even. Given a valuen, you will have to find how many integers from1tonhaveevenvalue ofσ.
Input
Input starts with an integerT (≤ 100), denoting the number of test cases.
Each case starts with a line containing an integern (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 中,所有數的約數和是偶數的數的個數。
有一個理論:
偶數 * 偶數 = 偶數;
奇數 * 偶數 = 偶數;
奇數* 奇數 = 奇數;
整數唯一分解定理:x = p1^a1*p2^a2*……* pk^ak
因數和公式 σ(x)= (1+p1+p1^2+……+p1^a1) * (1+p2+p2^2+……+p2^a2) *……* (1+pk+pk^2+……+pk^ak) (說明一下:題目中的式子因數和公式就是有由當前這個式子推匯出來的)
我們的目的就是要使(1+pi+pi^2+……+pi^ai)這個東西都是奇數,這樣就可以確定這個因數和為奇數。
現在考慮兩種情況(因為有一個質因子是2,所以要單獨考慮它)
⑴質因子為2的時候,當pi=2的情況這部分等比數列求和一定是奇數。
⑵pi是其他,再觀察這個式子(1+pi+pi^2+……+pi^ai),我們發現這式子裡面每一項都是奇數,所以只有當ai是偶數的時候,(1+pi+pi^2+……+pi^ai)這個值才是奇數,所以我們回到整數唯一分解定理看x = p1^a1*p2^a2*……* pk^ak,我們只需要把ai的值都變成偶數即可,可以變成x = p1^(a1'*2)*p2^(a2'*2)*……* pk^(ak'*2) = (p1^a1'*p2^a2'*……* pk^ak')^2。
如果沒有2這個素因子一定是完全平方數,如果有2,相當於一個完全平方數*2的幾次方,所以它要麼也是一個完全平方數(2的偶數次方)或者2*一個完全平方數(2的奇數次方,提出一個2其他構成完全平方數)
AC程式碼:
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { int t,k=0; cin>>t; while(t--){ ll n,ans=0; cin>>n; for(ll i=1; i*i<=n; i++){ ans++; if(2*i*i<=n)ans++; } printf("Case %d: %lld\n",++k,n-ans); } return 0; }