首页 > 代码库 > HDU 5014Number Sequence

HDU 5014Number Sequence

思路:

对于一个二进制100011;

尽量将填满:填成111111;

然后有一个很好算的方法

gets(n)表示二进制下N有多少位,N^X=(111111)2

X=111111^N;

其实答案可以直接 ANS=N*(N+1);找的规律;

#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>using namespace std;typedef long long ll;int n;int num(int x){    int k=0;    while (x)    {        k++;        x/=2;    }    return k;}int b[100003];int a[100003];int main(){    int n;    while (scanf("%d",&n)!=EOF)    {        memset(b,0,sizeof(b));        for (int i=n;i>=0;i--)        if (!b[i]){        int k=((1<<num(i))-1)^i;        b[i]=k;        b[k]=i;        }                printf("%I64d\n",(ll)(n+1)*n);        for (int i=0;i<=n;i++) scanf("%d",&a[i]);        for (int i=0;i<n;i++) printf("%d ",b[a[i]]);        printf("%d\n",b[a[n]]);    }    return 0;}

 

HDU 5014Number Sequence