BZOJ1081: [SCOI2005]超級格雷碼

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   for   

1081: [SCOI2005]超級格雷碼Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 237  Solved: 121
[Submit][Status]Description

著名的格雷碼是指2n個不同n位位元(即0~2n-1,不足n位在前補零)的一個排列,這個排列滿足相鄰的兩個位元的n位元字中最多隻有一個數字不同(例如003和001就有一個數位不同,而003和030有兩個數位不同,不符合條件)。例如n=2時,(00,01,11,10)就是一個滿足條件的格雷碼。 所謂超級格雷碼就是指Bn個不同的n位B進位數的排列滿足上面的條件。 任務:給出n和B(2≤B≤36, 1≤Bn≤65535),求一個滿足條件的格雷碼。對於大於9的數位用A~Z表示(10~35)。

Input

只有一行,為兩個整數n和B。

Output

一共Bn個行,每行一個B進位數,表示你所求得的合格排列

Sample Input2 2Sample Output00
01
11
10HINT

請不要提交此題...

Source

題解:

類似與一直給一個數+1,然後進位的時候比較特殊,要一位一位退。。。

代碼:

 1 #include<cstdio> 2  3 #include<cstdlib> 4  5 #include<cmath> 6  7 #include<cstring> 8  9 #include<algorithm>10 11 #include<iostream>12 13 #include<vector>14 15 #include<map>16 17 #include<set>18 19 #include<queue>20 21 #include<string>22 23 #define inf 100000000024 25 #define maxn 10000026 #define maxm 500+10027 28 #define eps 1e-1029 30 #define ll long long31 32 #define pa pair<int,int>33 34 #define for0(i,n) for(int i=0;i<=(n);i++)35 36 #define for1(i,n) for(int i=1;i<=(n);i++)37 38 #define for2(i,x,y) for(int i=(x);i<=(y);i++)39 40 #define for3(i,x,y) for(int i=(x);i>=(y);i--)41 42 #define mod 100000000743 44 using namespace std;45 46 inline int read()47 48 {49 50     int x=0,f=1;char ch=getchar();51 52     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}53 54     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}55 56     return x*f;57 58 }59 int n,m,cs,a[maxn],b[maxn];60 61 int main()62 63 {64 65     freopen("input.txt","r",stdin);66 67     freopen("output.txt","w",stdout);68 69     n=read();m=read();cs=1;70     for1(i,n)a[i]=0,b[i]=1,cs*=m;71     while(cs--)72     {73         for1(i,n)printf("%c",a[i]<10?a[i]+‘0‘:a[i]-10+‘A‘);printf("\n");74         for1(i,n)75          if(a[i]+b[i]>-1&&a[i]+b[i]<m){a[i]+=b[i];break;}else b[i]*=-1;76     }77 78     return 0;79 80 }
View Code

 

BZOJ1081: [SCOI2005]超級格雷碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.