I, as a ACMer, always take algorithm complexity into consideration when programming. Today, I introduce you some elegant algorithms of root Complex.
1. s(n)=∑ni=1⌊ni⌋
Since The range of ⌊ni⌋ contains at most 2√n . There may exist a algorithm of complexity O(√n).
1 | LL getsum(LL n){ // The code is simple and easy to understand |
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16LL 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)=n∑i=1σk(n)=n∑i=1∑d|idk=∑d=1dkn∑i[d|i]=n∑d=1dk⌊nd⌋If we get ts[n]=∑ni=1ik, similar to problem 1, we have fllowing C++ code:
1 | LL getf(LL n){ |
Actuall, we have
ts[n]={n(n+1)2k=1n(n+1)(2n+1)6k=2n2(n+1)24k=3
and for general
you can see my blog for detail.
4. g(n)=∑ni=1ϕ(i)
Throughout this blog, ϕ(n) donate the Euler function unless explicitly stated.ϕ(n) counts the positive integers less than or equal to n that are relatively prime to n. Euler’s product formula:
ϕ(n)=n∏p|n(1−1p)
where the product is over the distinct prime numbers dividing n.
Now, we begin to computer g(n)
g(n)=n∑i=1ϕ(i)=∑1≤x≤y≤n,gcd(x,y)=11
we define
gk(n)=n∑i=1ϕ(i)=∑1≤x≤y≤n,gcd(x,y)=k1=f(⌊nk⌋)
and ∑ni=1gk(i)=∑1≤x≤y≤n1=n(n+1)2 So we have
g(n)=n(n+1)2−n∑k=2f(⌊nk⌋)Similar to problem 1,we have,
1 | const int N=1000006; |
5. Problem hdu5608
If
n2−3n+2=∑d|nf(d)
calculate
h(n)=n∑i=1f(i)mod109+7
Since
n∑i=1∑d|if(d)=n∑i=1f(i)⌊ni⌋=n∑i=1h(⌊ni⌋)
and we have,
by conditon. Hence,
h(n)=n(n−1)(n−2)3−n∑i=2h(⌊ni⌋)
1 | //#pragma comment(linker,"/STACK:10240000,10240000") |
6. Note
All C++ code above should be changed to fit different demands.Thanks Zimpha who provides some problems and ideas two years old.