Poj-2115-c looooops (Extended Euclid)

Source: Internet
Author: User

C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19826 Accepted: 5299

Description

A Compiler mystery:we is given a c-language style for loop of type
for (variable = A; variable! = B; variable + = C)  statement;

i.e., a loop which starts by setting variable to value A and while variable are not equal to B, repeats statement followed By increasing the variable by C. We want to know what many times does the statement get executed for particular values of A, B and C, assuming this all Arit Hmetics is calculated in a k-bit unsigned integer type (with values 0 <= x < 2k) modulo 2k.

Input

The input consists of several instances. Each instance are described by a A, a and four integers a, B, C and K separated by a single space. The integer k (1 <= k <=) is the number of bits of the control variable of the loop and A, B, C (0 <= A, B, c < 2k) is the parameters of the loop.

The input is finished by a line containing four zeros.

Output

The output consists of several lines corresponding to the instances on the input. The i-th line contains either the number of executions of the statement in the I-th instance (a single integer number) or The word FOREVER if the loop does not terminate.

Sample Input

3 3 2 163 7 2 167 3 2 163 4 2 160 0 0 0

Sample Output

0232766FOREVER

Source

CTU Open 2004



or expand Euclid, here to simplify the original equation


AC Code:

#include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string> #include <vector> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long# Define INF 0x7fffffffusing namespace std; ll GCD (ll A, ll b) {return b = = 0? a:gcd (b, a% b);} void Exgcd (ll A, ll B, ll& X, ll& y) {if (b = = 0) {x = 1; y = 0;} else {EXGCD (b, a% B, y, x); y-= x * (A/b);}} int main () {LL A, B, C, K;while (scanf ("%i64d%i64d%i64d%i64d", &a, &b, &c, &k)! = EOF) {if (A = = 0 && Amp b = = 0 && C = 0 && k = 0) break;if (A = = B) {printf ("0\n"); continue;} LL a = C; LL B = (1LL << k); LL C = gcd (A, b); LL d = b-a;if (d% c! = 0) {printf ("forever\n"); continue;} A/= c;//here to be simplified, because the extra number of times B/= c;d/= C may be generated; LL p, Q;EXGCD (A, B, p, q);//A set of X, Y ("%i64d\n") for the simplest ax+by=gcd (A, a), (p * (D/GCD (A, b))% B + b)% B);} return 0;}












Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Poj-2115-c looooops (Extended Euclid)

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.