http://lightoj.com/volume_showproblem.php?problem=1336
Sigma Function
Time Limit:2000MS
Memory Limit:32768KB
64bit IO Format:%LLD &%llusubmit Status Practice Lightoj 1336
Description
Sigma function is a 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 and for large numbers it's very difficult to find in a straight forward. But mathematicians has 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'll have the to find what many integers from 1 to N has even value O F σ.
Input
Input starts with an integer T (≤100), denoting the number of test cases.
Each case is 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
The main idea: give you a number n, let you ask from 1 to n the number of factors and even numbers, you can use the formula of the unique division theorem
To look for (I wrote this, but timed out)
Then we can call out the number of unsatisfied numbers in 100 by the code that is timed out, as follows:
2 3
1 1
4 7
8 15
9 13
16 31
18 39
25 31
32 63
36 91
49 57
50 93
64 127
72 195
81 121
98 171
100 217
Found in 100, the factors and the odd number are:
1 2 4 6 9 16 18 25 32 36 49 50 64 72 81 98 100
Have found that these numbers have a law, they are x^2, 2*x, 2*x^2, as long as 100 of the number to meet one of these three, then, this number is not satisfied,
Total-Number of unsatisfied = number of satisfied
We can also find that when X is even, 2*x and x^2 have duplicate parts.
When x is odd, 2*x and 2*x^2 have duplicate parts.
Then we can save 2*x, we find out the number of x^2 and the number of 2*x^2, and then use the total minus their number can be
AC Code:
#include <stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespaceStd;typedefLong Longll;Const intN = 1e6 +Ten;intMain () {intT, p =0; ll N; scanf ("%d", &t); while(t--) {p++; ll x, y; scanf ("%lld", &N); X= (ll) sqrt (n);//calculates the number of x^2 in ny = (ll) sqrt (1.0N2);//calculates the number of 2*x^2 in nprintf"Case %d:%lld\n", p, N-x-y); } return 0;}
The Tle code (which can be used to make the table look regular):
#include <stdio.h>#include<math.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespaceStd;typedefLong Longll;Const intN = 1e6 +Ten;intPrime[n], K;BOOLIsprime[n];voidPrime () {k=0; memset (IsPrime,true,sizeof(IsPrime)); isprime[1] =false; for(inti =2; i < N; i++) { if(Isprime[i]) {prime[k++] =i; for(intj =2; J * I < N; J + +) Isprime[j* I] =false; }}}ll Solve (ll N) {ll x, Y, A; ll Num=1; for(LL i =0; I < K && Prime[i] * Prime[i] <= N; i++) {x=0; if(n% prime[i] = =0) { while(n% prime[i] = =0) {x++; N/=Prime[i]; } x+=1; A=1; for(LL j =1; J <= X; J + +) A*=Prime[i]; Y= A-1; Num*= y/(Prime[i]-1); } } if(N >1) Num*= ((n * N-1)/(N-1)); returnnum;}intMain () {Prime (); intT, x =0; ll N; scanf ("%d", &t); while(t--) {x++; scanf ("%lld", &N); ll Num=0; for(LL i =2; I <= N; i++) {ll m=solve (i); if(M%2!=0) {printf ("%lld%lld\n", I, M); Num++; }} printf ("Case %d:%lld\n", X, N-1-num); } return 0;}
Lightoj 13361336-sigma Function (Finding law + unique decomposition theorem)