Poj2115:c looooops (unary linear with congruence)

Source: Internet
Author: User

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

The first two days were solved by a linear equation of two yuan, and the optimal solution was obtained by using the extended Euclidean original aim.

Analysis:

Number theory is not too much to understand, the solution of the time, met with no small trouble.

Set the answer to X,n = (1<<k), then (a+c*x)% n = = B

IE (a+c*x) ≡b (mod n)//-----Results are obvious on both sides (A+CX)%n==b<n

Degenerate C*x≡ (b-a) (mod n)//----with Comodule nature a-c==b-c (mod n) on a==b (mod n)

oneself dizzy, or master of bad, and before the code, just the method of derivation more one.

#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;Long Longa,b,c,k;Long Longx1,x2;Long LonggcdLong LongALong Longb) {    returnb==0? A:GCD (b,a%b);}voidExtendLong LongALong LongBLong Long&AMP;X1,Long Long&y1) {    if(b==0) {x1=1; Y1=0; return ; } Extend (B,a%b,x1,y1); Long Longt=X1; X1=Y1; Y1=t-(A/b) *Y1;}intMain () { while(SCANF ("%lld%lld%lld%lld", &a,&b,&c,&k)! =EOF) {        if(a==0&&b==0&&c==0&&k==0) Break; Long LongA=C; Long LongB=pow (2, K); Long Longc=b-A; Long Longtemp=gcd (A, b); if(c%temp) {printf ("forever\n"); Continue; } A/=temp; B/=temp; C/=temp;        Extend (A,B,X1,X2); Long Longt= (c*x1%b+b)%C; printf ("%lld\n", T); }    return  0;}

Poj2115:c looooops (unary linear with congruence)

Related Article

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.