Input

The first line of input consists of an integers T where 1≤T≤1000, the number of test cases. Then follow T lines, each containing four integers a, n, b, m satisfying 1≤n,m≤10e9, 0≤a<n, 0≤b<m. Also, you may assume gcd(n,m)=1.
Output

For each test case, output two integers x, K, where K=n*m and 0≤x<K, giving the solution x(mod K) to the equations x=a(mod n),x=b(mod m).

Sample Input 1  Sample Output 1
2
1 2 2 3
151 783 57 278
5 6
31471 217674

感谢Pursuit_大神的一波支援。

由 ( x ≡ a )%n 以及  (x≡ b)%m这两个同余方程。可以联立得出一个二元一次方程—— k0*m+k1*(-n) = a-b。

然后就是解这个二元一次方程,得出最优解。对n*m取余。

直接上扩展欧几里德就好。

//Asimple
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, a, b; void ex_gcd( ll a , ll b , ll &g , ll &x , ll &y ) { if( b == ) {
x = ; y = ;
g = a ;
}
ex_gcd( b , a%b , g , y , x ) ;
y-= x*(a/b);
} void slove(){
ll x , y , g ;
ex_gcd( m , -n , g , x , y ) ;
x =( x*(a-b)/g %(-n /g ) - n/g )%(-n/g);
printf( "%lld %lld\n" , ((x * m + b)%(n*m)+ n*m )%(n*m) , n*m ) ;
} void input(){
int t ;
scanf( "%d" , &t ) ;
while( t-- ) {
scanf( "%lld%lld%lld%lld" , &a , &n , &b , &m ) ;
slove( ) ;
}
} int main(){
input();
return ;
}

Kattis之旅——Chinese Remainder的更多相关文章

  1. hdu 1788 Chinese remainder theorem again(最小公倍数)

    Problem Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,-,mk两两互素,则下面同余方程组: x≡a1(mod m1) x≡a2( ...

  2. Chinese remainder theorem again(中国剩余定理)

    C - Chinese remainder theorem again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:% ...

  3. DHU 1788 Chinese remainder theorem again 中国剩余定理

    Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  4. 中国剩余定理(Chinese Remainder Theorem)

    我理解的中国剩余定理的含义是:给定一个数除以一系列互素的数${p_1}, \cdots ,{p_n}$的余数,那么这个数除以这组素数之积($N = {p_1} \times  \cdots  \tim ...

  5. HDU 1788 Chinese remainder theorem again

    题目链接 题意 : 中文题不详述. 思路 : 由N%Mi=(Mi-a)可得(N+a)%Mi=0;要取最小的N即找Mi的最小公倍数即可. #include <cstdio> #include ...

  6. Kattis之旅——Prime Reduction

    A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...

  7. Kattis之旅——Fractional Lotion

    Freddy practices various kinds of alternative medicine, such as homeopathy. This practice is based o ...

  8. Kattis之旅——Factovisors

    The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...

  9. Kattis之旅——Rational Arithmetic

    Input The first line of input contains one integer, giving the number of operations to perform. Then ...

随机推荐

  1. Python 数据结构 树

    什么是树 数是一种抽象的数据类型(ADT)或是作这种抽象数据类型的数据结构,用来模拟具有树状结构性质的数据集合,它是由n(n>1)的有限个节点和节点之间的边组成的一个有层次关系的集合. 树的组成 ...

  2. nginx反向代理 支持WebSocket

    WebSocket(简称WS)协议的握手和HTTP是兼容的,通过HTTP/1.1中协议转换机制,客户端可以传递名为“Upgrade” 头部信息将连接从HTTP连接升级到WebSocket连接 那么反向 ...

  3. 截取字符串后几位用 length

  4. 《Java程序设计》第一周学习记录(1)

    目录 Windows安装JDK.Git Linux下安装JDK.Git.IDEA 参考资料 Windows安装JDK.Git 到官网直接下载JDK,双击安装程序就正常安装就行了. 下载完以后,可以看到 ...

  5. Cocos Creator 为Button添加事件的两种方法

    Button添加事件 Button 目前只支持 Click 事件,即当用户点击并释放 Button 时才会触发相应的回调函数.通过脚本代码添加回调方法一这种方法添加的事件回调和使用编辑器添加的事件回调 ...

  6. VS 应用模板 所交税和实发工资的运算

    double SFGZ, SL, SSKCS, YFGZ,a,YJS; //应发工资(基本工资),税率,速算扣除数,应发工资,判断标准,交多少税 //double QZD = 3500;//起征点 无 ...

  7. supervison

    http://blog.csdn.net/kongxx/article/details/50452357

  8. OWASP top 10

    OWASP Top 10 A1: InjectionSolution+Validate User Input+Never concatenate queries and date+Parameteri ...

  9. tcl脚本

    tcl,全名tool command language,是一种通用的工具语言. 1)每个命令之间,通过换行符或者分号隔开: 2)tcl的每个命令包含一个或者多个单词,默认第一个单词表示命令,第二个单词 ...

  10. QT自定义消息

    1.派生QAbstractNativeEventFilter,重写nativeEventFilter()函数 #ifndef CMYMESSAGEHANDLER_H #define CMYMESSAG ...