Poj 2115 C looooops (solving model linear equation)

Source: Internet
Author: User

Link: poj 2115

C-language cyclic statements for (I = A; I! = B; I + = C ),

Q: How many times does it end in a K-bit storage system.

If it ends within a limited period of time, the number of loops is output; otherwise, the endless loop is output.


Note: The data features of the K-bit storage system are used for loop (overflow)

For example, if the int type is 16 bits, the int type can store 2 ^ 16 data records,

That is, the maximum number is 65535 (the default value is unsigned ),

When the loop causes I to exceed 65535, I will return 0 and start counting again.

If I = 65534, When I + = 3, I = 1 that is, I = (65534 + 3) % (2 ^ 16) = 1


Analysis: if a group of data needs to be cyclically completed X times, the following equation is available:

X = [(B-A + 2 ^ K) % 2 ^ K]/C

That is, Cx = (B-A) (mod 2 ^ K) This equation is a model linear equation, This is the value of X.

That is, modulus linear equation.Ax = B mod n. Where a = C, B = B-A, n = 2 ^ K.


The sufficient and required conditions for the equation to be resolved are gcd (A, n) | B, that is, B % gcd (A, n) = 0.

So that D = gcd (A, n) has the smallest integer of this equation and returns X0 = x (mod n/D)

X is an arbitrary solution, and x0 is the smallest integer solution.


# Include <stdio. h >__ int64 exgcd (_ int64 A ,__ int64 B ,__ int64 & X ,__ int64 & Y) // Extended Euclidean {_ int64 T, D; if (B = 0) {x = 1; y = 0; return a;} d = exgcd (B, A % B, x, y); t = X; X = y; y = T-A/B * Y; return D;} int main () {_ int64 A, B, C, K, A, B, n, d, X, Y; while (scanf ("% i64d % i64d % i64d % i64d", & A, & B, & C, & K )! = EOF) {if (a = 0 & B = 0 & C = 0 & K = 0) break; A = C; B = B-A; N = (_ int64) 1 <K; // 1 to convert to 64-bit D = exgcd (A, n, x, y); If (B % d) {// If the equation has no solution, printf ("Forever \ n"); continue;} X = x * (B/d) % N; X = (X % (n/D) + (n/D) % (n/d); // returns the smallest positive solution printf ("% i64d \ n ", x);} return 0 ;}


Poj 2115 C looooops (solving model linear equation)

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.