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 ...
随机推荐
- sap 查看类的修饰属性
- spring框架中的注解
- Django-分页、中间件和请求的声明周期
一.分页 相关连接:https://www.cnblogs.com/kongzhagen/p/6640975.html 一.Django的分页器(paginator) 1.view.py 视图 fro ...
- syslog-ng内容讲解
一.基础syslog-ng作为syslog的替代工具,可以完全替代syslog的服务,并且通过定义规则,实现更好的过滤功能.系统自带版本: 引用 # rpm -qa|grep syslog-ngsys ...
- [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- c# 判断文件是否发生了变化
你这个是想文件发生改变时,自动调用一个函数,做出一些操作呢. 还是有一个按钮(或者别的什么),你去点击一下,然后检测下一个文件,是否发生了变化? 下面的代码,监控d盘下的所有.txt文件的修改 1 2 ...
- 深入解密.NET(GC垃圾回收)
值类型与引用类型 值类型(Value Type),值类型实例通常分配在线程的堆栈(stack)上,并且不包含任何指向实例数据的指针,因为变量本身就包含了其实例数据 C#的所有值类型均隐式派生自Syst ...
- Locust性能测试
https://www.cnblogs.com/yoyoketang/p/9638151.html https://www.cnblogs.com/yoyoketang/p/9642242.html ...
- bowtie2 Linux安装
目前最新版本为2.3.2,网址为:https://sourceforge.net/projects/bowtie-bio/files/bowtie2/2.3.2 安装分为简单的下载可执行文件和源编译安 ...
- H5 dom元素保存为图片
一.使用插件html2canvas:https://github.com/niklasvh/html2canvas 具体代码: 1.html <div class="test" ...