【t014】拯数

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t014

【题意】

【题解】

这个锁的序列,如果把末尾的0去掉;
然后再倒过来;
那么就是这个序列对应的格雷码了;
然后
根据格雷码和二进制的对应关系;
能够处理出对应的二进制;
比如格雷码存在a数组,二进制存在b数组;
则
b[1] = a[1]
rep1(i,2,n)
   b[i] = b[i-1]^a[i];
然后把这个二进制转成十进制就是答案了
要写高精度哦


【完整代码】

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1100;

struct abc
{
    int len, a[N];
    abc()
    {
        memset(a, 0, sizeof a);
    }
};

int n,a[N],b[N];
abc two[N], ans;

abc plu(abc a, abc b)
{
    int len = max(a.len, b.len);
    abc c;
    int x = 0;
    rep1(i, 1, len)
    {
        c.a[i] = a.a[i] + b.a[i] + x;
        x = c.a[i] / 10;
        c.a[i] %= 10;
    }
    while (x)
    {
        c.a[++len] = x;
        c.a[len] %= 10;
        x /= 10;
    }
    c.len = len;
    return c;
}

int main()
{
    //freopen("F:\rush.txt", "r", stdin);
    rei(n);
    rep1(i, 1, n)
        cin >> a[i];
    while (n - 1 >= 1 && a[n] == 0) n--;
    reverse(a + 1, a + 1 + n);
    b[1] = a[1];
    rep1(i, 2, n)
        b[i] = b[i - 1] ^ a[i];
    rep1(i, 1, n)
        a[i] = b[i];
    two[0].len = 1, two[0].a[1] = 1;
    rep1(i, 1, n)
    {
        two[i].len = two[i - 1].len;
        rep1(j, 1, two[i].len)
            two[i].a[j] = two[i - 1].a[j];
        int &len = two[i].len,x = 0;
        rep1(j, 1, len)
        {
            two[i].a[j] = two[i].a[j] * 2 + x;
            x = two[i].a[j] / 10;
            two[i].a[j] %= 10;
        }
        while (x > 0)
        {
            two[i].a[++len] = x;
            two[i].a[len] %= 10;
            x = x / 10;
        }
    }
    ans.a[1] = 0, ans.len = 1;
    rep2(i, n, 1)
    if (a[i])
        ans = plu(ans, two[n - i]);
    rep2(i, ans.len, 1)
        printf("%d", ans.a[i]);
    puts("");
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626582.html