Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! 参与排名人数12044
[codeforces 1362C] Johnny and Another Rating Drop 打表+按位找规律
总目录详见https://blog.csdn.net/mrcrack/article/details/103564004
在线测评地址http://codeforces.com/contest/1362/problem/C
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
C - Johnny and Another Rating Drop | GNU C++17 | Accepted | 46 ms | 200 KB |
打表结果如下
20
n=1 cnt=1
n=2 cnt=3
n=3 cnt=4
n=4 cnt=7
n=5 cnt=8
n=6 cnt=10
n=7 cnt=11
n=8 cnt=15
n=9 cnt=16
n=10 cnt=18
n=11 cnt=19
n=12 cnt=22
n=13 cnt=23
n=14 cnt=25
n=15 cnt=26
n=16 cnt=31
n=17 cnt=32
n=18 cnt=34
n=19 cnt=35
n=20 cnt=38
打表代码如下
#include <stdio.h>
#include <string.h>
int a[2][64];
int f(int x){
int top=0,pos=x%2;
while(x){
a[pos][top++]=x%2;
x/=2;
}
}
int main(){
int n,i,j,k,cnt;
scanf("%d",&n);
for(k=1;k<=n;k++){
cnt=0,memset(a,0,sizeof(a));
for(i=1;i<=k;i++){
f(i);
for(j=0;j<=63;j++)
if(a[i%2][j]!=a[(i-1)%2][j])cnt++;
}
printf("n=%d cnt=%d\n",k,cnt);
}
return 0;
}
找规律,还是要有一定的敏感度,
n=10^18,要么有固定公式,要么按位找规律。
以n=7为例找规律
位置210
(0)000
(1)001
(2)010
(3)011
(4)100
(5)101
(6)110
(7)111
可以看到:
第0位上相邻的7种情况中,数据不同的数量是7
第1位上相邻的7种情况中,数据不同的数量是7/2=3
第2位上相邻的7种情况中,数据不同的数量是7/2/2=1
总的不同数量是7+3+1=11
AC代码如下
#include <stdio.h>
#define LL long long
LL ans,n;
int main(){
int t;
scanf("%d",&t);
while(t--){
ans=0;
scanf("%lld",&n);
while(n){
ans+=n;
n/=2;
}
printf("%lld\n",ans);
}
return 0;
}