Kattis之旅——Chinese Remainder
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 |
5 6 |
感谢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的更多相关文章
- hdu 1788 Chinese remainder theorem again(最小公倍数)
Problem Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,-,mk两两互素,则下面同余方程组: x≡a1(mod m1) x≡a2( ...
- Chinese remainder theorem again(中国剩余定理)
C - Chinese remainder theorem again Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:% ...
- DHU 1788 Chinese remainder theorem again 中国剩余定理
Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 中国剩余定理(Chinese Remainder Theorem)
我理解的中国剩余定理的含义是:给定一个数除以一系列互素的数${p_1}, \cdots ,{p_n}$的余数,那么这个数除以这组素数之积($N = {p_1} \times \cdots \tim ...
- HDU 1788 Chinese remainder theorem again
题目链接 题意 : 中文题不详述. 思路 : 由N%Mi=(Mi-a)可得(N+a)%Mi=0;要取最小的N即找Mi的最小公倍数即可. #include <cstdio> #include ...
- Kattis之旅——Prime Reduction
A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...
- Kattis之旅——Fractional Lotion
Freddy practices various kinds of alternative medicine, such as homeopathy. This practice is based o ...
- Kattis之旅——Factovisors
The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...
- Kattis之旅——Rational Arithmetic
Input The first line of input contains one integer, giving the number of operations to perform. Then ...
随机推荐
- vuex 子组件传值
以下是基础的使用方法,详细且深入使用方法详细见博客:https://segmentfault.com/a/1190000015782272 Vuex官网地址:https://vuex.vuejs.or ...
- docker的容器和镜像的差别
- BiLSTM学习
转自:https://blog.csdn.net/aliceyangxi1987/article/details/77094970 https://blog.csdn.net/jojozhangju/ ...
- canvas 写一个刮刮乐抽奖
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- shell实现SSH自动登陆【转】
前言 公司开发使用docker,每次登陆自己开发机总要输入 ssh user_name@ip_string,然后再确认输入password,手快了还经常会输错.作为一个懒人,肯定要找一个取巧的方式,查 ...
- 如何使用js判断当前页面是pc还是移动端打开的
1.利用了正则表达式和三目运算符,含义就是如果是移动端打开的话那就跳转到 "https:www.baidu.com/" ,如果不是就跳转到"http://new.baid ...
- Browsersync结合gulp和nodemon实现express全栈自动刷新
Browsersync能让浏览器实时.快速响应你的文件更改(html.js.css.sass.less等)并自动刷新页面.更重要的是 Browsersync可以同时在PC.平板.手机等设备下进项调试. ...
- Css预处理器---Less(二)
三.Less语法 (1)变量 //less代码 @nice-blue : #5B83AD; @light-blue : @nice-blue + #111; #header { color : @li ...
- 011-Server服务器对象属性
Transfer:第一个页面直接调用第二个页面,执行完第二个页面后不再返回第一个页面,立即响应到客户端浏览器.Execute:第一个页面直接调用第二个页面,执行完第二个页面后再返回第一个页面执行,最后 ...
- python 定义类 简单使用
在test.py文件里面 #coding=utf-8 #类的定义 class user: #定义私有属性 __name = '' __age = 0 #定义基本属性 sex = '' #定义构造函数 ...