POJ2115 C-loop

Source: Internet
Author: User

Transmission Door

This problem is a good practice to solve the indefinite equation.

The topic is very strange. Also said what K-system, in fact, is to ask for a number a, each add C, ask how many times B to add, all the number of 2k modulo.

That means we can list the following equations: A+xc≡b (mod 2k)
We convert the equations on both sides of the equation, then we can get a form of the indefinite equation.

Old routines, judging there is no solution, otherwise with EXGCD solution.

There is a small hole in the 1<<32, you in the time, even if the variable opened Longlong also not, must be written in 1ll form, otherwise it will WA.

Take a look at the code.

#include <cstdio>#include<algorithm>#include<cstring>#include<iostream>#include<cmath>#include<Set>#include<vector>#include<queue>#definePB Push_back#defineRep (i,a,n) for (int i = a;i <= n;i++)#definePer (i,n,a) for (int i = n;i >= a;i--)#defineEnter Putchar (' \ n ')using namespaceStd;typedefLong Longll;Const intM =40005;Const intN =2000005;Const intINF =1000000009;Constll mod =51123987; ll read () {ll ans=0, op =1; CharCH =GetChar ();  while(Ch <'0'|| CH >'9')    {    if(ch = ='-') op =-1; CH=GetChar (); }     while(Ch >='0'&& CH <='9') {ans*=Ten; Ans+ = CH-'0'; CH=GetChar (); }    returnAns *op;} ll A,b,c,k,x,y,p,q,s;ll gcd (ll a,ll b) {return(!b)? A:GCD (b,a%b);} ll EXGCD (ll A,ll b,ll&x,ll &y) {    if(!b) {x=1, y =0; returnA; } ll D= EXGCD (b,a%b,y,x); Y-= A/b *x; returnD;}intMain () { while(1) {a= Read (), B = Read (), C = Read (), k =read (); if(!a &&!b &&!c &&!k) Break; P= C,q = 1LL << k,s = (b-a+q)%Q; ll G=gcd (P,Q); if(s%G) {printf ("forever\n"); Continue; } P/= g,q/= g,s/=G;    EXGCD (P,q,x,y); X= (x + q)% q,x *= s,x%=Q; printf ("%lld\n", x); }    return 0;}

POJ2115 C-loop

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.