[codeforces 1362C] Johnny and Another Rating Drop 打表+按位找规律

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

ProblemLangVerdictTimeMemory
C - Johnny and Another Rating Drop GNU C++17Accepted46 ms200 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值