POJ 2115 C Looooops Extended Euclidean and pojlooooops

Source: Internet
Author: User

POJ 2115 C Looooops Extended Euclidean and pojlooooops

It is not hard to understand the meaning of the question. The following formula can be obtained after reading the question:

(A + C * x-B) mod (2 ^ k) = 0

(C * x) mod (2 ^ k) = (B-A) mod (2 ^ k)

Use the modulus linear equation (linear homogeneous equation) to solve the problem.

Template direct car access

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace std; typedef long ll; ll exgcd (ll, ll B, ll & x, ll & y) {if (B = 0) {x = 1; y = 0; return a;} ll r = exgcd (B, a % B, y, x); ll t = x; y = y-a/B * t; return r;} bool modular_linear_equation (ll a, ll B, ll n) {ll x, y, x0, I; ll d = exgcd (a, n, x, y); if (B % d) {printf ("FOREVER \ n "); return fa Lse;} x0 = x * (B/d) % n; // x0 is a special solution of the equation, which can be positive or negative. The minimum non-negative ll ans is required. if (x0 <0) {ans = x0; for (I = 0; ans <0; I ++) ans = (x0 + I * (n/d) % n;} else {ans = x0; ll temp; for (I = 0; ans> = 0; I ++) {temp = ans; ans = (x0-I * (n/d) % n;} ans = temp;} printf ("% I64d \ n ", ans); return true;} int main () {// freopen ("in.txt", "r", stdin); ll A, B, C, k; while (scanf ("% I64d % I64d % I64d % I64d", & A, & B, & C, & k )) {if (A = 0 & B = 0 & C = 0 & k = 0) break; ll k2 = (ll) 1 <k; if (A = B) printf ("0 \ n"); else modular_linear_equation (C, B-A, k2);} return 0 ;}

 

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.