CSP-S 2019 格雷码

题面

 

 题解

 这道题,它都把格雷码的计算方法告诉我们了,所以真的只是个模拟题,

两种解法:①long long从负数存,要特判

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
LL cnt = 1,k2 = 0;
LL read() {
    LL f = 1,x2 = -9223372036854775808ll,x = 0;char s = getchar();
    while(s < '0' || s > '9') {if(s == '-')f = -1;s = getchar();}
    while(s >= '0' && s <= '9') {
        if(x >= 1e16) {
            cnt *= 10ll;
            k2 = k2 * 10ll + s - '0';
        }
        else x = x * 10 + s - '0';
        s = getchar();
    }
    if(cnt > 1) {
        cnt /= 10ll;
        x2 += k2;
        x2 += x * cnt;
        x2 += x * cnt;
        x2 += x * cnt;
        x2 += x * cnt;
        x2 += x * cnt;
        x2 += x * cnt;
        x2 += x * cnt;
        x2 += x * cnt;
        x2 += x * cnt;
        x2 += x * cnt;
    }
    else x2 += x * cnt + k2;
    return x2;
}
LL n,m,i,j,s,o,k;
bool flag = 0;
void dfs(LL n,LL k) {
//  printf("\n%lld %lld\n",n,k);
    if(n == 1) {
        printf("%d",(k + 9223372036854775807ll + 1ll) & 1ll);
        return ;
    }
    if(((k*1ll) > 1ll*((-9223372036854775808ll) + (1ll<<(n-2ll)) - 1ll + ((1ll<<(n-2ll)))) && !flag) || ((k*1ll) >= 1ll*((-9223372036854775808ll) + (1ll<<(n-2ll)) + ((1ll<<(n-2ll)))) && flag)) {
        putchar('1');
        k = k - (1ll<<(n-2ll));
        k = k - ((1ll<<(n-2ll)) - 1ll);
        return dfs(n - 1ll,(1ll<<(n - 2ll)) + (-9223372036854775808ll) + (1ll<<(n-2ll)) - k + (-9223372036854775808ll));
    }
    else {
        putchar('0');
        return dfs(n - 1ll,k);
    }
}
int main() {
//  freopen("code.in","r",stdin);
//  freopen("code.out","w",stdout);
    scanf("%lld",&n);
    k = read();
    if(k >= 0) flag = 1;
//  printf("%lld %lld\n, %d",k*1ll,1ll*((-9223372036854775808ll) + (1ll<<(n-2ll)) - 1ll + ((1ll<<(n-2ll)))),(k*1ll) > 1ll*((-9223372036854775808ll) + (1ll<<(n-2ll)) - 1ll + ((1ll<<(n-2ll)))));
    dfs(n,k);putchar('\n');
    return 0;
}

②(官方题解做法)用 unsigned long long :

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL unsigned long long
#define Un unsigned
using namespace std;
LL read() {
    LL f = 1,x = 0;char s = getchar();
    while(s < '0' || s > '9') {if(s == '-')f = -1;s = getchar();}
    while(s >= '0' && s <= '9') {
        x = x * 10 + s - '0';
        s = getchar();
    }
    return x;
}
LL n,m,i,j,s,o,k;
void dfs(LL n,LL k) {
    if(n == 1) {
        printf("%d",k & 1ll);
        return ;
    }
    if(k > (LL)((LL)(1ll)<<(n-1ll)) - (LL)(1ll)) {
        putchar('1');
        return dfs(n - 1ll,(LL)((LL)(1ll)<<(n-1ll)) - (LL)(1ll) + (LL)((LL)(1ll)<<(n-1ll)) - k);
    }
    else {
        putchar('0');
        return dfs(n - 1ll,k);
    }
}
int main() {
//  freopen("code.in","r",stdin);
//  freopen("code.out","w",stdout);
    scanf("%llu",&n);
    k = read();
    dfs(n,k);putchar('\n');
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值