C - Johnny and Another Rating Drop
阿新 • • 發佈:2020-07-03
C - Johnny and Another Rating Drop
這道題的題意看下面的註釋應該清楚了,這裡也不描述了(絕對不是我不知道這麼描述QAQ)
題解:這道題一開始不知道怎麼做,然後列了幾個資料就明瞭了
000000 000001 1 1 000010 1+2 3 000011 1+2+1 4 000100 1+2+1+3 7 000101 1+2+1+3+1 8 000110 1+2+1+3+1+2 10000111 1+2+1+3+1+2+1 11 001000 1+2+1+3+1+2+1+4 15 001001 1+2+1+3+1+2+1+4+1 16 001010 1+2+1+3+1+2+1+4+1+2 18 001011 1+2+1+3+1+2+1+4+1+2+1 001100 1+2+1+3+1+2+1+4+1+2+1+3 001101 1+2+1+3+1+2+1+4+1+2+1+3+1 001110 1+2+1+3+1+2+1+4+1+2+1+3+1+2 001111 1+2+1+3+1+2+1+4+1+2+1+3+1+2+1 010000 1+2+1+3+1+2+1+4+1+2+1+3+1+2+1+5 010001 1+2+1+3+1+2+1+4+1+2+1+3+1+2+1+5+1 if(n%2==0)sum+=n/2*cnt; else sum+=(n+1)/2*cnt;
這幾組我們不難發現奇數位上都是1,然後將這些1刪去,奇數位上全是2,把2刪去,奇數位上全是3……
所以公式為
if(n%2==0)sum+=n/2*cnt;
else sum+=(n+1)/2*cnt;//cnt 為刪去的次數
程式碼如下
#include<iostream> #include<cstdio> #include<algorithm> usingnamespace std; typedef long long LL; int main(){ int t; scanf("%d",&t); while(t--){ LL n; scanf("%lld", &n); LL k = n; LL sum = 0; LL cnt = 1; while(k!=0){ if(k%2==0) sum += cnt * (k / 2); else sum += cnt * ((k + 1) / 2); k = k / 2; cnt++; } printf("%lld\n", sum); } }