有趣的數(動態規劃)

來源:互聯網
上載者:User

標籤:ccf 有趣的數 動態規劃

問題描述

我們把一個數稱為有趣的,若且唯若:

1. 它的數字只包含0, 1, 2, 3,且這四個數字都出現過至少一次。

2. 所有的0都出現在所有的1之前,而所有的2都出現在所有的3之前。

3. 最高位元字不為0。

因此,符合我們定義的最小的有趣的數是2013。除此以外,4位的有趣的數還有兩個:2031和2301。

請計算恰好有n位的有趣的數的個數。由於答案可能非常大,只需要輸出答案除以1000000007的餘數。

輸入格式

輸入只有一行,包括恰好一個正整數n (4 ≤ n ≤ 1000)。

輸出格式

輸出只有一行,包括恰好n 位的整數中有趣的數的個數除以1000000007的餘數。

範例輸入

4

範例輸出

3


動態規劃解法:

import java.util.*;public class Main {      public static void main(String[] args) {      new Main().run();}public void run() {      Scanner fin = new Scanner(System.in);      int N = fin.nextInt();      long[] count = new long[8];      count[6] = 0;      count[7] = 1;      long mod = 1000000007;      for (int i = 2; i <= N; ++i) {          long[] newCount = new long[8];          newCount[0] = (count[0] * 2 + count[1] + count[3]) % mod;          newCount[1] = (count[1] * 2 + count[2] + count[5]) % mod;          newCount[2] = (count[2] + count[6]) % mod;          newCount[3] = (count[3] * 2 + count[4] + count[5]) % mod;          newCount[4] = (count[4] + count[7]) % mod;          newCount[5] = (count[5] * 2 + count[6] + count[7]) % mod;          newCount[6] = 0;          newCount[7] = 1;          count = newCount;      }     System.out.println(count[0]);   }}


本文出自 “閑庭信步、” 部落格,請務必保留此出處http://macxiao.blog.51cto.com/9606147/1587773

有趣的數(動態規劃)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.