POJ 2115-c looooops (basic unary linear with congruence)

Source: Internet
Author: User

Test instructions

For C's for (i=a; i!=b; I +=c) loop, ask how many loops in the K-bit storage system will end.

If the end is within a finite number of times, the output cycle times.

Otherwise output forever

Ideas:

Easy to list the congruence equation:

X*c+y*2^k = B-a can be solved by expanding GCD

148k0msc++808b#include<cstdio> #include <iostream> #include <algorithm> #include <cstring> Using namespace Std;typedef long long ll;ll a,b,c;ll x,y;int k;ll e_gcd (ll a,ll b,ll &x,ll &y) {    ll ans;    if (b==0)    {        ans=a;        x=1,y=0;    }    else    {        ans=e_gcd (b,a%b,y,x);        y-= (A/b) *x;    }    return ans;} int main () {    while (scanf ("%i64d%i64d%i64d%d", &a,&b,&c,&k), (a+b+c+k))    {        ll m= (LL) 1 <<k;        ll r= (b-a+m)%m;        ll D=E_GCD (c,m,x,y);        if (r%d) puts ("FOREVER");        else        {            ll ans=x* (r/d);            ll t=m/d;            Ans= (ans%t+t)%t;            printf ("%i64d\n", ans);        }    }    return 0;}



POJ 2115-c looooops (basic unary linear with congruence)

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.