POJ 2115 C Looooops Extended Euclid

Source: Internet
Author: User

C Looooops
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 23616 Accepted: 6517

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 Test Instructions: (A+BX)% (2^k) ==c, for x minimum; idea: the solution expands Euclid;
#include <iostream>#include<string>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;#definell Long Long#defineESP 1E-13Const intn=1e4+Ten, m=1e6+50000, inf=1e9+Ten, mod=1000000007;voidExtend_euclid (ll A, ll B, ll &x, LL &y) {    if(b = =0) {x=1; Y=0; return; } extend_euclid (b, a%b, x, y); LL TMP=x; X=y; Y= tmp-(A/b) *y;} ll GCD (ll A,ll b) {if(b==0)        returnA; returnGCD (b,a%b);} ll Pow1 (ll x) {ll sum=1;  for(LL i=0; i<x;i++) Sum*=2; returnsum;}intMain () {ll x,y,i,z,t;  while(~SCANF ("%lld%lld%lld%lld",&x,&y,&i,&t)) {if(x==0&&y==0&&i==0&&t==0)         Break; ll M=Pow1 (t); ll c= ((y-x)%m+m)%m;        ll J,k; if(C%GCD (m,i) = =0) {extend_euclid (i,m,j,k); ll ans=j* (c/gcd (m,i)); M=m/gcd (m,i); printf ("%lld\n", (ans%m+m)%m); }        Elseprintf ("forever\n"); }    return 0;}

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.