Back Gear | NOIP2012 equation for the same remainder

Source: Internet
Author: User

Title Description Description
The minimum positive integer solution for the congruence equation ax≡1 (mod b) of x is obtained.
Input/output format Input/output
Input format:
Enter only one row, containing two positive integers a, b, separated by a space.
Output format:
The output has only one row and contains a positive integer x0, which is the minimum positive integer solution. The input data guarantees that there must be a solution.
Input: 3 7

Output: Ten

The problem: We should understand test instructions first. The title means ax Modb=1, or ax-kb=1. Consider the extended Euclidean theorem. Obviously y can be a negative value, and our goal is to find a minimum positive integer x, which makes AX 1 larger than an integer of B. the title meaning is: Ax=1+by, also namely Ax-by=1.

Code:
#include"iostream"#include"Cstdio"#include"Cstdlib"#include"CString"#include"algorithm"using namespacestd;Long Longa,b,x,y;Long LongEXTENDED_GCD (Long LongALong LongBLong Long&x,Long Long&y) {if(!B) {x=1; y=0;returnA;} Else    {              Long LongANS=EXTENDED_GCD (b,a%b,x,y); Long LongT=x; X=y; Y=t-(A/b) *y; returnans; } } intMain () {CIN>> a >>b; Long LongAns =EXTENDED_GCD (A, B, X, y); X=x%b;  while(x<0) x+=b; //The x we find is an integer multiple of a, and this integer multiple can be negative. But the requirement is the smallest integer multiples//so we'll add X plus B, because plus we can still meet the requirements, that is, Ax MoD b=1. cout << x <<Endl; return 0; } 

Back Gear | NOIP2012 equation for the same remainder

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.