HDU 1005 number sequence (loop section)

Source: Internet
Author: User


First, it is easy to find a circular section when a table is violent. Therefore, the statement at the beginning is to directly find the circular section. The result is always wa,

The reason is that some cycles are not starting from 1, 1, 1. Detailed proof here: http://acm.hdu.edu.cn/discuss/problem/post/reply.php? Postid = 19818 & messageid = 1 & Deep = 0

So I learned from the idea of the great gods. Because of % 7, V [7] [7] can be used to record f (n) = (A * F (n-1) + B * F (n-2) mod 7. this status. If the status is the same, the loop section appears.

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1005

The Code is as follows:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <cctype> # include <algorithm> # include <string> # define n 100 using namespace STD; int V [7] [7]; int f [N] = {0, 1}; int main () {int A, B, N; while (scanf ("% d", & A, & B, & N), A + B + n) {int x = 1, y = 1, k = 3; memset (v, 0, sizeof (v); While (! V [x] [Y]) {v [x] [Y] = K; // records the position where this status occurs. F [k] = (A * Y + B * X) % 7; X = y; y = f [k]; k ++ ;} int S = V [x] [Y]; // This is important because the cycle may not start from. If (n <k) printf ("% d \ n", F [N]); else printf ("% d \ n", F [(n-s) % (k-S) + S]); // K-S is the cycle T. } Return 0 ;}


HDU 1005 number sequence (loop section)

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.