中国剩余定理模板 51nod 1079
题目链接:传送门
推荐博客:https://www.cnblogs.com/freinds/p/6388992.html (证明很好,代码有误)。
第1行:1个数N表示后面输入的质数及模的数量。(2 <= N <= 10)
第2 - N + 1行,每行2个数P和M,中间用空格分隔,P是质数,M是K % P的结果。(2 <= P <= 100, 0 <= K < P)
输出符合条件的最小的K。数据中所有K均小于10^9。
3
2 1
3 2
5 3
23
模板:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<cstdio>
using namespace std;
#define eps 1e-8
#define ll long long
#define INF 0x3f3f3f3f
ll m[],a[],x,y,d,n;
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b)
{
d=a;
x=;
y=;
return;
}
ex_gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
ll china()
{
ll M=,ans=;
for(int i=;i<n;i++)
M*=m[i]; //求出所有数的乘积
for(int i=;i<n;i++)
{
ll w=M/m[i]; //w是除m[i]外其他数的最小公倍数,全是质数情况下
ex_gcd(w,m[i],d,x,y); //求公式w*x+m[i]*y=1的解,因为如果x是w*x%m[i]=1的解,那么x*w*a[i]%m[i]=a[i]
ans=(ans+x*w*a[i])%M;
}
return (ans+M)%M;
}
int main()
{
cin>>n;
for(int i=;i<n;i++)
cin>>m[i]>>a[i];
cout<<china()<<endl;
return ;
}
中国剩余定理模板 51nod 1079的更多相关文章
- 51nod 1079 中国剩余定理模板
中国剩余定理就是同余方程组除数为质数的特殊情况 我直接用同余方程组解了. 记得exgcd后x要更新 还有先更新b1再更新m1,顺序不能错!!(不然会影响到b1的更新) #include<cstd ...
- Monkey Tradition---LightOj1319(中国剩余定理模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 题意:有 n 个猴子,n 棵树,树的高度为 L ,每个猴子刚开始的时候都在树的底 ...
- poj 1006中国剩余定理模板
中国剩余定理(CRT)的表述如下 设正整数两两互素,则同余方程组 有整数解.并且在模下的解是唯一的,解为 其中,而为模的逆元. 模板: int crt(int a[],int m[],int n) { ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- [洛谷P1495] 曹冲养猪 (中国剩余定理模板)
中国剩余定理(朴素的)用来解线性同余方程组: x≡a[1] (mod m[1]) x≡a[2] (mod m[2]) ...... x≡a[n] (mod m[n]) 定义ms=m[1]*m[2]*. ...
- 中国剩余定理模板&俄罗斯乘法
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){ if(!b){d=a;x=1LL;y=0LL;} else {ex_gcd(b,a%b,d, ...
- 中国剩余定理模板poj1006
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #i ...
- [Luogu P4777] 【模板】扩展中国剩余定理(EXCRT) (扩展中国剩余定理)
题面 传送门:洛咕 Solution 真*扩展中国剩余定理模板题.我怎么老是在做模板题啊 但是这题与之前不同的是不得不写龟速乘了. 还有两个重点 我们在求LCM的时候,记得先/gcd再去乘另外那个数, ...
- 51nod1079 poj2891 中国剩余定理与其扩展
题目链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1079 一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K. ...
随机推荐
- Python3里查看某一元素的源码(检查元素定位是否准确)
#coding:utf-8 #显示网页元素的HTML源码from selenium import webdriver driver = webdriver.Ie()driver.implicitly_ ...
- kubernetes安装过程报错及解决方法
1.your configuration file uses an old API spec: "kubeadm.k8s.io/v1alpha2". 执行kubeadm init ...
- leetcode41
package main import ( "fmt" ) func firstMissingPositive(nums []int) int { m := make(map[in ...
- <基础> PHP 数组操作
array_filter — 用回调函数过滤数组中的单元 ( 如果 callback 函数返回 true,则 array 数组的当前值会被包含在返回的结果数组中.数组的键名保留不变 ) array a ...
- vue语法小练习
实现功能:新增/删除 学生 <html> <head> <script src="https://cdn.staticfile.org/vue/2.2.2/vu ...
- Django从MySQL数据库生成model
字段太多的话,手动建表,然后用 inspectdb 命令生成model文件,效率会高很多: inspectdb 表名 >> model文件名.py >> 是追加在文件末尾:& ...
- datetime is not json serializable
python, datetime is not json serializable import datetime def json_serial(obj): """JS ...
- The value for the useBean class attribute xxx is invalid
JSP页面报这个错可能的原因: 1:指定的 Bean 类没找到 2:该类不是 public 的,或者找到的 class 文件是 interface 或抽象类 3:Bean 类中没有 public 的无 ...
- 一个linux内核模块移植到低版本时发生的异常
在3.10的内核版本下,有一个运行稳定的内核模块,移植到suse11的时候,编译正常,运行则直接出现crash: <>[ <>[ 503.347300] Modules lin ...
- Excel VBA 使用笔记
开发工具选项卡(Developer Tab) File Tab -> Options -> Customize Ribbon -> Main Tabs -> Developer ...