I, as a ACMer, always take algorithm complexity into consideration when programming. Today, I introduce you some elegant algorithms of root Complex.
转载请注明Reproduced please specifydna049.com
1. s(n)=∑ni=1⌊ni⌋
Since The range of ⌊ni⌋ contains at most 2n‾‾√ . There may exist a algorithm of complexity O(n‾‾√) .
LL getsum(LL n){ // The code is simple and easy to understand
LL sum = 0;
for(LL i=1,j;i<=n;i=j+1){
j = n/(n/i);
sum += (j-i+1)*(n/i);
}
return sum;
}
Actually, s(n) donate the number of positive integer point under graph xy=1 .
2. σk(n)=∑d|ndk
- σ0(n) donate the number of divisors.
- σ1(n) donate the sum of divisors.
LL mypow(LL x,LL n){
LL r = 1;
while(n){
if(n&1) r=r*x;
n>>=1; x=x*x;
}
return r;
}
LL getr(LL n,LL k){
LL r = 0,d;
for(d=1;d*d<n;++d){
if(n%d==0) r += mypow(d,k) + mypow(n/d,k);
}
if(d*d == n) r+=mypow(d,k);
return r;
}
The code above is primary and trival.
3. f(n)=∑ni=1σk(i)
f(n)=∑i=1nσk(n)=∑i=1