Sgu 181. X-Sequence

Source: Internet
Author: User

Time Limit: 0.5 s

Space limit: 4 m

Question:

Make X0 =,

Xi = (A * Xi-1 ^ 2, B * Xi-1 + C) % m;

Calculate XK, (0 <= k <= 109), (0 <= A, B <= 100), (1 <m <1000 );

 

 

 

Solution:

The key to the question lies in the scope of M. The range of 1000 is obviously cyclical after the remainder is obtained.

You only need to get the length of the loop sequence and each of its numbers, and the starting position of any loop. You can calculate the XK value.

Note that x0 does not return M remainder, and when x0 is large, the intermediate amount of computing X1 may exceed the int range.

 

Code:

 

# Include <iostream> # include <string> # include <cstring> # include <vector> using namespace STD; const int INF = 111111; vector <int> ans; int f [INF]; long X, A, B, C, M, K, T; inline long GET (long X) {return (A * x + B * x + C) % m;} int main () {CIN> x> A> B> C> m> K; // locate the end of the first loop, that is, for (t = 0 ;! F [X]; X = get (x), t ++) {f [x] = 1; if (t = k) {cout <X; return 0 ;}} K-= (t-1); // the new position of K from the starting point of the loop // get the cyclic array memset (F, 0, sizeof F ); for (t = 0 ;! F [X];) {f [x] = 1; ans. push_back (x); X = get (x), t ++;} // locate K in the loop section and output k = K % t-1; if (k <0) k = t-1; cout <ans [k]; return 0 ;}

 

  

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.