#include <iostream> using namespace std; int main(){ long n;
int i,j; int sum = 0; while(cin>>n){ i = 1;
while(n > 1){ if(n%2 == 1){ sum += (n+1)*(n+1)/4;
n = (n-1)/2; } else{ i = n;
while(i%2 == 0) i /= 2; sum += i;
} } sum++; cout<<sum<<endl; }
return 0; }
def MaxPrime(N):
while(N % 2 == 0):
N = N / 2
return N
def sumMaxPrime(N):
sum = 0
for x in xrange(1, N + 1):
if x % 2 != 0:
sum = sum + x
else:
sum = sum + MaxPrime(x)
print sum
if __name__ == '__main__':
sumMaxPrime(400)
#include "iostream"
using namespace std;
typedef long long LL;
int main()
{
LL N;
LL Ans = 0;
while (cin >> N)
{
Ans = 0;
for (LL i = N; i > 0; i = i / 2)
Ans += ((i + 1) / 2) * ((i + 1) / 2);
cout << Ans << endl;
}
return 0;
}
我是这么写的,up可以参考一下。注意公式里面要套扩考,优先级错了,结果会出问题