UVa 10359-tiling

Source: Internet
Author: User

Title: give you a 2*n ground, with 1*2 and 2*2 floor tile paved, how many different plans.

Analysis: Combinatorial mathematics, dynamic programming. Direct find to push relationship solver.

Because, it is only possible that the last column is a whole (1 cases) or the last two columns are a whole (two cases);

So, there is a recursive formula: F (n) = f (n-1) + 2*f (n-2);

It can be solved using dynamic programming or a parent function (an = (POW (2,n+1)-pow ( -1,n+1))/3).

Description: Large integer operation, here using DP solution, seemingly fast power will quickly ╮(╯▽╰)╭.

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace std;int ans[255][101],two[101];void copy_array (int *a, int *b) {for (int i = 0; I < 100; + + i) a[i] = B[i];} void Add_array (int *c, int *a, int *b) {for (int i = 0; I < + + i) c[i] = 0;for (int i = 0; I < + + i) {C[i] + = A[i]+b[i];if (C[i] > 9) {c[i+1] + c[i]/10;c[i]%= 10;}} void Output_array (int *a) {int end = 100;while (end &&!a[end])--end;while (end >= 0) printf ("%d", a[end--]);p rintf ("\ n");} int main () {memset (ans, 0, sizeof (ans)); ans[0][0] = 1;ans[1][0] = 1;for (int i = 2; i < 252; + + i) {Add_array (II, Ans[i -2], ans[i-2]); Add_array (Ans[i], ans[i-1], and);} int N;while (~SCANF ("%d", &n)) Output_array (Ans[n]);    return 0;}


UVa 10359-tiling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.