Johnny and Another Rating Drop(找规律)

传送门

思考:

  • n的范围都到10^18了,肯定是有什么公式或者是找规律的题目

在纸上写写画画一通,貌似找到了规律,但是总结的时候又没对

看了题解才发现自己规律的方向找错了

  • 对于一个二进制为n位的数字,其最右边一位总贡献是n
  • 从右往左数第二位总贡献是n / 2

..............................
那么规律就找出来了,n不断除二,时间复杂度是O(logn)

#include <bits/stdc++.h>
using namespace std;

int main(){
	int t;
	cin >> t;
	while(t --){
		long long n;
		cin >> n;
		long long ans = 0;
		while(n){
			ans += n;
			n /= 2;
		}
		cout << ans << endl;
	}
	
	return 0;
} 
原文地址:https://www.cnblogs.com/pureayu/p/14420261.html