POJ2680(动态规划,大数)

Computer Transformation

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4548   Accepted: 1731

Description

A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, after the first time step, the sequence 0 1 is obtained; after the second, the sequence 1 0 0 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on.

How many pairs of consequitive zeroes will appear in the sequence after n steps?

Input

Every input line contains one natural number n (0 < n <= 1000).

Output

For each input n print the number of consequitive zeroes pairs that will appear in the sequence after n steps.

Sample Input

2
3

Sample Output

1
1

Source

Southeastern Europe 2005

思路详见:动态规划

java

import java.util.*;
import java.math.*;

public class Main {

    public static void main(String[] args) {
        final int maxn = 1010;
        BigInteger fa[] = new BigInteger[maxn];
        BigInteger fb[] = new BigInteger[maxn];
        BigInteger TWO = BigInteger.valueOf(2);
        fa[0] = fb[0] = BigInteger.ZERO;
        for (int i = 1; i < maxn; i ++) {
            fa[i] = fa[i-1].add(fb[i-1]);
            fb[i] = fa[i-1].add(fb[i-1]).add(BigInteger.valueOf(i).mod(TWO));
        }
        int n;
        Scanner cin = new Scanner(System.in);
        while (cin.hasNext()) {
            n = cin.nextInt();
            System.out.println(fb[n-1]);
        }
    }
}

Python

import sys
fa = [0] * 1010
fb = [0] * 1010
fa[0] = 0;
fb[0] = 0;
for i in range(1, 1010):
    fa[i] = fa[i-1] + fb[i-1]
    fb[i] = fa[i-1] + fb[i-1] + i % 2

for line in sys.stdin:
    n = int(line)
    print(fb[n-1])
时间: 2024-12-23 06:42:24

POJ2680(动态规划,大数)的相关文章

ACM学习历程—HDU 3092 Least common multiple(数论 &amp;&amp; 动态规划 &amp;&amp; 大数)

hihoCoder挑战赛12 Description Partychen like to do mathematical problems. One day, when he was doing on a least common multiple(LCM) problem, he suddenly thought of a very interesting question: if given a number of S, and we divided S into some numbers

动态规划网络资料收集

之前在学习如何写hoj 1003 的时候,由于不懂动态规划,所以把动态规划相关内容智力学习了一下,虽然对我来说,学会1003和看明白跟动态规划没有什么比较大的关系,但对之前收集的资料做一下整理也好. 首先,什么是动态规划,如何理解动态规划 知乎徐凯强的答案很有“总纲”的感觉 https://www.zhihu.com/question/23995189/answer/35324479 王勐 也说的很清楚 https://www.zhihu.com/question/23995189/answer

NI笔试——大数加法

NI笔试: 1.找出字符串第一次出现的字符.用数组建立哈希表,然后再扫描字符串并判断次数是否为1. 2.大数加法,即字符串加法.因为之前写过乘法,就以为是乘法.然后就把乘法写上去了····= = 好了,看一下加法的思路. 要不要太简单,用俩数组,先把字符串每个位转换成数字存到这俩数组里,然后对每一位进行加和. 代码是拿别人的.= = void Add(char s1[],char s2[]) //需要两个字符串参数&&无返回值 { int num1[M],nm2[M]; int i,j;

集训第五周动态规划 H题 回文串统计

Hrdv is interested in a string,especially the palindrome string.So he wants some palindrome string.A sequence of characters is a palindrome if it is the same written forwards and backwards. For example, 'abeba' is a palindrome, but 'abcd' is not.A pa

[SinGuLaRiTy] 动态规划题目复习

[SinGuLaRiTy-1026] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metro 题目描述 特工玛利亚被送到S市执行一个特别危险的任务.她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂. 玛利亚有一个任务,现在的时间为0,她要从第一个站出发,并在最后一站的间谍碰头.玛利亚知道有一个强大的组织正在追踪她,她知道如果一直呆在一个车站,她会有很大的被抓的风险,躲

大数问题,通常用JAVA

e.g. HDU1002 import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin=new Scanner(System.in); int t=cin.nextInt(); cin.nextLine(); int cnt=0; while(t!=0) { t--; String s1=cin.next

【UOJ#22】【UR #1】外星人(动态规划)

[UOJ#22][UR #1]外星人(动态规划) 题面 UOJ 题解 一道简单题? 不难发现只有按照从大往小排序的顺序选择的才有意义,否则先选择一个小数再去模一个大数是没有意义的. 设\(f[i][j]\)表示考虑了前\(i\)个数,模完之后是\(j\)的方案数. 转移的时候枚举这个数是模还是不模,如果不模的话就要把它放到后面某个小数的后面,方案数是\(n-i\). #include<iostream> #include<cstdio> #include<cstdlib>

Leetcode 494 Target Sum 动态规划 背包+滚动数据

这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20))和一个数S c1a1c2a2c3a3......cnan = S, 其中ci(1<=i<=n)可以在加号和减号之中任选. 求有多少种{c1,c2,c3,...,cn}的排列能使上述等式成立. 例如: 输入:nums is [1, 1, 1, 1, 1], S is 3. 输出 : 5符合要求5种

活动选择的贪心算法与动态规划(未完成)

// greedy_algorithm.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include<queue> using namespace std; #define NofActivity 11 int c[NofActivity + 1][NofActivity + 1]; int reme[NofActivity + 1][NofActivity + 1]; //活动的