大致题意:现有两种方块(1X2,2X2),方块数量无限制。问用这两种方块填满2Xn的矩阵的填法有多少种。

分析:通俗点说,找规律。专业化一点,动态规划。

  状态d[i],表示宽度为i的填法个数。

  状态转移方程:d[0]=d[1]=1,i=0,1;

         d[i]=d[i-1]+d[i-2]*2;

  c或者c++写的话,先模拟下大数吧,java直接用大数类就行了。

c++

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; struct BigInteger
{
int num[100];
int len;
};
BigInteger add(BigInteger a,BigInteger b)
{
BigInteger c;
c.len=0;
int len=max(a.len,b.len);
int left=0;
for(int i=0; i<len; i++,c.len++)
{
int tmp=a.num[i]+b.num[i]+left;
c.num[i]=tmp%10;
left=tmp/10;
}
if(len==a.len)
for(int i=len; i<b.len; i++,c.len++)
{
int tmp=b.num[i]+left;
c.num[i]=tmp%10;
left=tmp/10;
}
if(len==b.len)
for(int i=len; i<a.len; i++,c.len++)
{
int tmp=a.num[i]+left;
c.num[i]=tmp%10;
left=tmp/10;
}
if(left) c.num[c.len++]=1;
return c;
}
int main()
{
//freopen("in.txt","r",stdin);
int n;
BigInteger a,b,c;
while(cin>>n)
{
if(n==0 || n==1)
printf("1\n");
else
{
a.num[0]=a.len=1;
b.num[0]=b.len=1;
c.len=0;
for(int i=2; i<=n; i++)
{
c=add(add(a,a),b);
a=b;
b=c;
}
for(int i=c.len-1;i>=0;i--)
printf("%d",c.num[i]);
printf("\n");
}
}
return 0;
}

java

import java.math.BigInteger;
import java.util.Scanner; public class Main { public static void main(String args[]) {
int n;
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
n = scanner.nextInt();
BigInteger a = BigInteger.ONE;
BigInteger b = BigInteger.valueOf(3);
if (n == 1 || n == 0)
System.out.println(a);
else if (n == 2)
System.out.println(b);
else {
BigInteger ans = BigInteger.ZERO;
for (int i = 0; i < n - 2; i++) {
ans = a.multiply(BigInteger.valueOf(2)).add(b);
a = b;
b = ans;
}
System.out.println(ans);
}
}
} }

POJ 2506 Tiling dp+大数 水题的更多相关文章

  1. poj 3080 Blue Jeans(水题 暴搜)

    题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...

  2. HDU-1042-N!(Java大法好 &amp;&amp; HDU大数水题)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Subm ...

  3. POJ 3176 Cow Bowling (水题DP)

    题意:给定一个金字塔,第 i 行有 i 个数,从最上面走下来,只能相邻的层数,问你最大的和. 析:真是水题,学过DP的都会,就不说了. 代码如下: #include <cstdio> #i ...

  4. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  5. DP+贪心水题合集_C++

    本文含有原创题,涉及版权利益问题,严禁转载,违者追究法律责任 本次是最后一篇免费的考试题解,以后的考试题目以及题解将会以付费的方式阅读,题目质量可以拿本次作为参考 本来半个月前就已经搞得差不多了,然后 ...

  6. poj 1007:DNA Sorting(水题,字符串逆序数排序)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 80832   Accepted: 32533 Des ...

  7. poj 1004:Financial Management(水题,求平均数)

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 126087   Accepted: ...

  8. poj 1658 Eva's Problem(水题)

    一.Description Eva的家庭作业里有很多数列填空练习.填空练习的要求是:已知数列的前四项,填出第五项.因为已经知道这些数列只可能是等差或等比数列,她决定写一个程序来完成这些练习. Inpu ...

  9. poj 2506 Tiling(递推 大数)

    题目:http://poj.org/problem?id=2506 题解:f[n]=f[n-2]*2+f[n-1],主要是大数的相加; 以前做过了的 #include<stdio.h> # ...

随机推荐

  1. -bash: ll: 未找到命令

    第一步将alias ll='ls -l'添加到/etc/profile # head /etc/profile# /etc/profilealias ll='ls -l'# System wide e ...

  2. 优秀电路资料--- IOT方案

    完整的IOT方案 http://www.cirmall.com/circuit/4117/%E3%80%90%E5%BC%80%E6%BA%90%E3%80%91%E5%AE%8C%E6%95%B4% ...

  3. java_[类加载器]

    Class 与 类加载 Class对象由JVM自动产生,每当一个类被加载时,JVM就自动为其生成一个Class对象,通过Class对象可以获得类的相关信息. 将类信息读到内存中过程,称为类加载 Jav ...

  4. 国内镜像源 sources

    Ubuntu18.04源 cat > /etc/apt/sources.list <<eof # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb ...

  5. 第7讲 | ICMP与ping:投石问路的侦察兵

    第7讲 | ICMP与ping:投石问路的侦察兵 ping 是基于 ICMP 协议工作的.ICMP 全称 Internet Control Message Protocol,就是互联网控制报文协议. ...

  6. LogMysqlApeT

    with LogMysqlApeT(db) as m_client: condition = "select * from {} where deleted=0 ".format( ...

  7. Linux的top命令cpu占用少,但是显示很高

    最近发现服务器一个奇怪的问题,40核的双路服务器,装的centos7.4系统,开机过几个小时后会图形界面特别卡顿,top里发现CPU使用率50%左右,但是进程里没有大量占用的进程.怎么上传不了图片.. ...

  8. AI推理与Compiler

    AI推理与Compiler AI芯片编译器能加深对AI的理解, AI芯片编译器不光涉及编译器知识,还涉及AI芯片架构和并行计算如OpenCL/Cuda等.如果从深度学习平台获得IR输入,还需要了解深度 ...

  9. TVMNN编译Compiler栈

    TVMNN编译Compiler栈 内容纲要 前言 调研目标 TVM介绍 TVM源码架构 FrontEnd Relay BackEnd VTA实现原理及设计思想提炼 整体结构 VTA Hardware ...

  10. 视频系列:RTX实时射线追踪(上)

    视频系列:RTX实时射线追踪(上) Video Series: Practical Real-Time Ray Tracing With RTX RTX在游戏和应用程序中引入了一个令人兴奋的和根本性的 ...