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. 控制input框只能粘贴,不能输入

    .禁用文本框的onkeydown事件 <input type="text" onkeydown="return false"> .改造,可以使用ct ...

  2. Flappy Bird

    在网上学习了下“65行 JavaScript 代码实现 Flappy Bird 游戏”(http://blog.jobbole.com/61842/),main.js 如下: // Initializ ...

  3. jQuery UI 拖拽-拉伸

    jquery-ui实现 官网:http://www.runoob.com/jqueryui/example-resizable.html layui实现 demo:http://www.jq22.co ...

  4. root_objectlist, root_object, container_objectlist, container_object 之间的关系。

  5. keycloak

    keycloak报错, 少了配置项   keycloak.enabled=ture 找不到 publicKey,  1   ping不通 认证中心,2 网络不好

  6. [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. Tensorflow实现LeNet-5、Saver保存与读取

    一. LeNet-5 LeNet-5是一种用于手写体字符识别的非常高效的卷积神经网络. 卷积神经网络能够很好的利用图像的结构信息. 卷积层的参数较少,这也是由卷积层的主要特性即局部连接和共享权重所决定 ...

  8. Entity Framework Code First(概要)

    EF开源项目地址:https://github.com/aspnet/EntityFramework6 MSDN :https://msdn.microsoft.com/en-us/library/a ...

  9. C# 指定http请求使用Tls1.2

    转载于 https://blog.csdn.net/yanghaitian/article/details/77498872 客户端语言 版本 类库 是否支持 兼容方案   Java 1.6.115之 ...

  10. vue路由(一个包含重定向、嵌套路由、懒加载的main.js如下)and 路由跳转传参的query和params的异同

    import Vue from 'vue'import VueRouter from 'vue-router'import App from './App'Vue.use(VueRouter)cons ...