poj 1006中国剩余定理模板
中国剩余定理(CRT)的表述如下
设正整数两两互素,则同余方程组
有整数解。并且在模下的解是唯一的,解为
其中,而
为
模
的逆元。
模板:
int crt(int a[],int m[],int n)
{
int e=;
int mod=;
for(int i=; i<=n; i++) mod*=m[i];
for(int i=; i<=n; i++)
{
int temp=mod/m[i];
e=(e+a[i]*temp*inv(temp,m[i]))%mod; // 这里求逆元 是对mi求,,
}
return e;
}
poj 1006ac代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int mod;
int exgcd(int a,int b,int &x,int &y)
{
if(b==)
{
x=;
y=;
return a;
}
int temp=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return temp;
}
int inv(int a,int b)
{
int x,y;
int temp=exgcd(a,b,x,y);
if(x<) x+=b/temp;
return x;
}
int crt(int a[],int m[],int n)
{
int e=;
int mod=;
for(int i=; i<=n; i++) mod*=m[i];
for(int i=; i<=n; i++)
{
int temp=mod/m[i];
e=(e+a[i]*temp*inv(temp,m[i]))%mod; // 这里求逆元 是对mi求,,
}
return e;
}
int main()
{
int a[],m[];
m[]=;
m[]=;
m[]=;
int s;
int Case=;
while(cin>>a[]>>a[]>>a[]>>s)
{
if(a[]==- && a[]==- && a[]==- && s==-) break;
int e=;
Case++;
e=crt(a,m,);
if(e <= s) e+=;
printf("Case %d: the next triple peak occurs in %d days.\n",Case,e-s);
}
return ;
}
poj 1006中国剩余定理模板的更多相关文章
- POJ 2891 中国剩余定理(不互素)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 17877 ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- 中国剩余定理模板 51nod 1079
题目链接:传送门 推荐博客:https://www.cnblogs.com/freinds/p/6388992.html (证明很好,代码有误). 1079 中国剩余定理 基准时间限制:1 秒 空间 ...
- Monkey Tradition---LightOj1319(中国剩余定理模板)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 题意:有 n 个猴子,n 棵树,树的高度为 L ,每个猴子刚开始的时候都在树的底 ...
- POJ 2891 中国剩余定理的非互质形式
中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...
- 51nod 1079 中国剩余定理模板
中国剩余定理就是同余方程组除数为质数的特殊情况 我直接用同余方程组解了. 记得exgcd后x要更新 还有先更新b1再更新m1,顺序不能错!!(不然会影响到b1的更新) #include<cstd ...
- [洛谷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, ...
- poj 1006 中国剩余定理解同余方程
其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...
随机推荐
- CISCO实验记录七:OSPF
一.要求 1.使用OSPF创建路由表 2.查看OSPF邻居 二.实现 1.使用OSPF创建路由表 #router ospf 1 #network 192.168.1.0 0.0.0.255 area ...
- pwn学习日记Day15 《程序员的自我修养》读书笔记
程序编译链接过程: 1.调用cc1程序,这个程序实际上就是GCC的C语言编译器,它将"hello.c"编译成一个临时的汇编文件"/tmp/ccUhtGSB.s" ...
- 性能分析 | Java服务器内存过高&CPU过高问题排查
一.内存过高 1.内存过高一般有两种情况:内存溢出和内存泄漏 (1)内存溢出:程序分配的内存超出物理机的内存大小,导致无法继续分配内存,出现OOM报错 (2)内存泄漏:不再使用的对象一直占据着内存不释 ...
- The first one spawns an additional process forwarding requests to a series of workers (think about it as a form of shield, at the same level of apache or nginx), while the second one sets workers to n
Things to know (best practices and “issues”) READ IT !!! — uWSGI 2.0 documentationhttps://uwsgi-docs ...
- 9Flutter GridView组件 以及动态GridView
main.dart import 'package:flutter/material.dart'; import 'res/listData.dart'; /* GridView : 通过GridVi ...
- eNSP V100R002C00B500 Setup模拟CE6800
本人操作系统:Windows7 64 下载需要的安装包: eNSP V100R002C00B500 官网下载地址:点击这里 CE6800 官网下载地址:点击这里 USG6000V ...
- [Graphics] UIColor created with component values far outside the expected range, Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.
用了别人的代码,一直总有一个报错,一开始没注意,最近项目快完期了,得处理下警告之类的东西, 后面发现之前那个大神代码是这样写的 [SVProgressHUD setBackgroundColor:[U ...
- Node.js使用Express实现Get和Post请求
var express = require('express'); var app = express(); // 主页输出 "Hello World" app.get('/', ...
- [!] The version of CocoaPods used to generate the lockfile (1.4.0.beta.1) is higher than the version of the current executable (1.3.0.beta.1). Incompatibility issues may arise.
今天在看一个开源Demo代码的时候,需要执行pod install命令,直接报错如下: 解决方法: 执行:pod update 命令更新资源库即可.
- Flutter磨砂玻璃效果制作
Flutter的Fliter Widget 也是非常强大的,它可以制作出你想要的神奇滤镜效果.下面制作一个毛玻璃效果. 这个和以前的写法都一样,所以就直接贴代码了. import 'package:f ...