http://poj.org/problem?id=1090 (题目链接)
题意:给出九连环的初始状态,要求将环全部取下需要走多少步。
Solution
格雷码:神犇博客
当然递推也可以做。
代码:
// poj1090 #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> #include<set> #define MOD 1000000007 #define inf 2147483640 #define LL long long #define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout); using namespace std; inline LL getint() { LL x=0,f=1;char ch=getchar(); while (ch>‘9‘ || ch<‘0‘) {if (ch==‘-‘) f=-1;ch=getchar();} while (ch>=‘0‘ && ch<=‘9‘) {x=x*10+ch-‘0‘;ch=getchar();} return x*f; } int n,a[2000],f[1010][400],t[400]; int main() { scanf("%d",&n); int sum=0; for (int i=1;i<=n;i++) { scanf("%d",&a[i]); sum+=a[i]; } for (int i=1;i<=n;i++) { sum-=a[i]; if (sum&1) a[i]=!a[i]; } f[1][1]=1; for (int i=2;i<=1000;i++) { for (int j=1;j<=399;j++) f[i][j]=f[i-1][j]*2; for (int j=1;j<=399;j++) { f[i][j]+=f[i][j-1]/10; f[i][j-1]%=10; } } for (int i=1;i<=n;i++) if (a[i]) { for (int j=1;j<=399;j++) t[j]+=f[i][j]; for (int j=1;j<=399;j++) { t[j]+=t[j-1]/10; t[j-1]%=10; } } int i; for (i=399;i>=1;i--) if (t[i]) break; if (i==0) printf("0"); else for (;i>=1;i--) printf("%d",t[i]); return 0; }
时间: 2024-10-11 19:35:03