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;
}

  

最新文章

  1. zoj 3888 线段树 ***
  2. coocs2d-x资源压缩笔记
  3. Apache 配置多端口网站
  4. 【分享】通过Excel生成批量SQL语句,处理大量数据的好办法
  5. hadoop2.2.0伪分布式搭建
  6. Redis和Memcache的区别分析 [转]
  7. webapi拦截请求
  8. rabbitmq问题之HTTP access denied: user &#39;guest&#39; - User can only log in via localhost
  9. 5.spark弹性分布式数据集
  10. 动态链接库(DLL)编写经验
  11. nginx配置默认首页(index.htnl index.htm)全流程(包含遇到问题的解决)
  12. BIZ中model.getSql源码分析
  13. Collection集合。
  14. 用switch组件控制一个元素的显示和隐藏状态
  15. RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa
  16. php语法基础(相比C语言)
  17. cocos2d JS-(JavaScript) 类型检测与判断
  18. hdu5266 pog loves szh III 【LCA】【倍增】
  19. Java Thread 如何处理未捕获的异常?
  20. Java 静态代码块&amp;构造代码块&amp;局部代码块

热门文章

  1. Oracle中没有 if exists(...)
  2. javascript之享元模式
  3. jquery.validate运用和扩展
  4. [转]Ubuntu 用vsftpd 配置FTP服务器
  5. [转]hadoop hdfs常用命令
  6. BibTex参考文献制作
  7. 20135208 第一次JAVA实验报告
  8. Linux 第一次学习笔记
  9. UIPasteboard的使用
  10. jsonp的后台怎么返回去数据