POJ2115 C looooops "Solving linear congruence equation"

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=2115


Main topic:

For loop statements:

for (int i = A; I! = B; i + = C)

Statement 1;

It is known that I, A, B, and C are all K-binary unsigned integer types, giving values of a, B, C, K, and calculating and outputting statements 1

The number of executions, if infinite, then the direct output "FOREVER".


Ideas:

set the algorithm to perform X-steps, then the title becomes solution A + cx≡b (mod M) (m= 2^k). that is a + CX + my≡b.

CX + MY ≡b-a (M = 2^k) is changed in order to find linear congruence equation, simple linear congruence algorithm can be applied.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace Std;__int64 a,b,c,k;__int64 a,b,c,d,x,y;void exgcd (__int64 a,__int64 b,__int64 &d,__int64 &x,__int64 &y) { C0/>if (!b)        x = 1, y = 0, d = A;    else    {        exgcd (b,a%b,d,y,x);        Y-= x * (A/b);    }} int main () {while    (cin >> A >> B >> C >> k && (a| | b| | c| | k))    {        a = C;        c = b-a;        b = (__int64) 1 << k;        EXGCD (a,b,d,x,y);        if (c% d! = 0)            cout << "FOREVER" << Endl;        else        {            __int64 ans,temp;            Ans = x * C/D;            temp = b/d;            ans = ans% temp + temp;            cout << ans% temp << endl;        }    }    return 0;}



POJ2115 C looooops "Solving linear congruence 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.