1.当
n=2k+1
:
f(n)=∑i=1n−1i⊗(n−i)=2∑i=1k2i⊗(n−2i)=2∑i=1k2i⊗(2k−2i+1)=4∑i=1ki⊗(k−i)+2k=4f(k)+6k
2.当
n=2k
:
f(n)=∑i=1n−1i⊗(n−i)=∑i=1k2i⊗(2k−2i)+∑i=1k(2i−1)⊗(n−2i+1)
对于左边那个式子:
∑i=1k2i⊗(2k−2i)=2∑i=1k−1i⊗(k−i)=∑i=1k−1i⊗(k−i)=2f(k)
对于右边那个式子:
∑i=1k(2i−1)⊗(n−2i+1)=∑i=1k−1(2i+1)⊗(n−2i−1)=∑i=0k−1(2i+1)⊗[2(k−1)−2i+1]=2{∑i=0k−1i⊗(k−1−i)}=4k−4+2[∑i=1k−2i⊗(k−1−i)]=4k−4+2∑i=0k−2i⊗(k−1−i)=2f(k−1)+4k−4
加上去就是
f(n)=2f(k)+2f(k−1)+4k−4
.
import java.math.*;
import java.util.*;
public class Main {
BigInteger x0 = BigInteger.valueOf (0);
BigInteger x1 = BigInteger.valueOf (1);
BigInteger x2 = BigInteger.valueOf (2);
BigInteger x3 = BigInteger.valueOf (3);
BigInteger x4 = BigInteger.valueOf (4);
BigInteger x5 = BigInteger.valueOf (5);
BigInteger x6 = BigInteger.valueOf (6);
Map <BigInteger, BigInteger> gg = new HashMap <BigInteger, BigInteger> ();
BigInteger dfs (BigInteger u) {
if (gg.get (u) != null) {
return gg.get (u);
}
BigInteger k = u.divide (x2);
BigInteger mo = u.mod (x2);
BigInteger ans;
if (mo.compareTo (x1) == 0) {
ans = dfs (k).multiply (x4).add (k.multiply (x6));
gg.put (u, ans);
return ans;
}
BigInteger kk = k.subtract (x1);
ans = dfs (k).multiply (x2).add (dfs (kk).multiply (x2)).add (k.multiply (x4)).subtract (x4);
gg.put (u, ans);
return ans;
}
void solve () {
Scanner cin = new Scanner (System.in);
gg.put (x2, x0); gg.put (x3, x6); gg.put (x4, x4);
while (cin.hasNext ()) {
BigInteger n = cin.nextBigInteger ();
n = dfs (n);
System.out.println (n);
}
}
public static void main (String[] args) {
Main work = new Main();
work.solve ();
}
}