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