[Mathematical problem] PKU 3472 holey square Tiling

Source: Internet
Author: User

 

First, we will introduce the idea of solving this question: classification. Be good at abstracting familiar models from complicated problems to solve them. Bytes. At the beginning, I listed the recurrence relationship of this question based on the idea of the poj monthly contest solution report and wrote the program myself. I found it very troublesome and submitted a timeout warning to me! In desperation, I can only look for other methods. I thought for a long time that there was no good progress. Finally, under the guidance of my senior brother, I referred to the above problem-solving report, this paper simplifies the recursive formula by using the properties of famous Fibonacci identities. // 4*(2 * F (n-1) ^ 2 + (-1) ^ (n-1) ^ 2 // n points odd or even discussed, but the running result is still incorrect. After debugging for a long time, there is no good result, after several hours of suffering, I accidentally changed short to int and found that the result was normal. It turned out to be a cross-border disaster! The submission was successful, more than 8000 ms. It is clear that such a program is difficult to pass during the general competition, so I started to improve the multi-precision algorithm. Indeed, the running time has been greatly improved. On the basis of the original 10-digit system, I used a 1000-digit system, reducing the program running time to more than 1000 ms. Indeed, the power of algorithms is infinite! Note that the first one uses printf ("% d") and later must use printf ("% 03d"). errors are prevented here. The following section describes the improvement of my high-precision algorithm: const int M = 1000;/high-precision Multiplication
Void mult (int * a, int * B, int * C)
{// C = A * B;
Int I, j, M, N, K;
M = A [0]; n = B [0];
K = N + m-1;
For (I = 0; I <= K; I ++) C [I] = 0;
For (I = 1; I <= m; I ++ ){
If (A [I]) for (j = 1; j <= N; j ++) if (B [J]) c [I + J-1] + = A [I] * B [J];
}
For (I = K; I> = 1; I --) if (C [I]> = m) {C [I-1] + = C [I]/m; c [I] % = m ;}////////////////
If (C [0]> 0)
{K ++; for (I = K; I> = 1; I --) C [I] = C [I-1];} C [0] = K;
} // High-precision addition void add (int * a, int * B, int * C)
{// C = A + B;
Int m, n, I, J, K;
M = A [0]; n = B [0];
If (M <n) k = N; else K = m;
For (I = 0; I <= K; I ++) C [I] = 0;
J = K;
For (I = N; I> = 1; I --, j --) C [J] = B [I];
J = K;
For (I = m; I> = 1; I --, j --) C [J] + = A [I];
For (I = K; I> = 1; I --)
{If (C [I]> = m) {C [I-1] ++; C [I]-= m ;} //////////////////////////////////
}
If (C [0])
{K ++; for (I = K; I> = 1; I --) C [I] = C [I-1];}
C [0] = K;
}

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.