C. Johnny and Another Rating Drop
解法
这种题,可以先打表找找规律。然后观察发现,每一个数相对于前一个数,都是由其进位得来的,想想一下一个数进位之后就只会把进位位由0变1,右边的1变成0,也就是之后涉及进位的部分发生了反转。就类似于反码+1变补码。
现在就能知道任何一个数跟前一个数的计算值了,比如:10001000,就是4,最后的0的个数+最后一个1
所以先计算末尾以1结束的个数 * 1 (x+1)/2 * 1
末尾以10结束的个数 * 2 ((x>>1) + 1)/2 * 2
末尾以100结束的个数 * 3 ((x>>2) + 1)/2 * 3
代码
#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug_in freopen("in.txt","r",stdin)
#define debug_out freopen("out.txt","w",stdout);
#define pb push_back
#define all(x) x.begin(),x.end()
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pii;
const ll maxn = 1e6+10;
const ll maxM = 1e6+10;
const ll inf = 1e8;
const ll inf2 = 1e17;
template<class T>void read(T &x){
T s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
x = s*w;
}
template<class H, class... T> void read(H& h, T&... t) {
read(h);
read(t...);
}
template <typename ... T>
void DummyWrapper(T... t){}
template <class T>
T unpacker(const T& t){
cout<<' '<<t;
return t;
}
template <typename T, typename... Args>
void pt(const T& t, const Args& ... data){
cout << t;
DummyWrapper(unpacker(data)...);
cout << '\n';
}
void out(__int128 x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x >= 10) out(x / 10);
putchar(x % 10 +'0');
}
//--------------------------------------------
void table(){
for(int i = 1;i<=100;i++){
int a = i-1,b = i,cnt = 0;
for(int j = 31;j>=0;j--){
if((a>>j) ^ (b>>j)) cnt++;
}
pt(i,cnt);
}
}
ll T,N;
void solve(){
__int128 ans = 0,cnt = 0;
while(N){
ans += (__int128)1 * (N+1)/2 * (++cnt);
N/=2;
}
out(ans);puts("");
}
int main(){
// debug_in;
read(T);
while(T--){
read(N);
solve();
}
return 0;
} 
京公网安备 11010502036488号