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" ...
随机推荐
- fastjson序列化和反序列化报com.alibaba.fastjson.JSONException: autoType is not support异常问题,解决方案整合
1.问题起因 2017年3月15日,fastjson官方发布安全升级公告,该公告介绍fastjson在1.2.24及之前的版本存在代码执行漏洞,当恶意攻击者提交一个精心构造的序列化数据到服务端时,由于 ...
- --thunder-lock is available since uWSGI 1.4.6 but never got documentation (of any kind)
--thunder-lock is available since uWSGI 1.4.6 but never got documentation (of any kind) Serializing ...
- create-react-app 工程,如何修改react端口号?
概要: 3000端口是webpack配置里面写的,可以通过传递一个PORT全局变量,来修改这个端口.当然,您还可以在node_modules/react-scripts/目录下面,批量搜索替换3000 ...
- 003-log-jul,jdk自带日志服务
一.简介 java官方日志jul,位于java.util.logging包下. 1.1.POM依赖 无依赖 1.2.配置 JUL的默认配置文件是logging.properties ,在 $JAVA_ ...
- Qt编写安防视频监控系统9-自动隐藏光标
一.前言 这个效果的灵感来自于大屏电子看板系统,在很多系统中尤其是上了大屏的时候,其实在用户不在操作的时候,是很不希望看到那个鼠标箭头指针的,只有当用户操作的时候才显示出来,这个就需要开个定时器定时计 ...
- org/springframework/cache/jcache/config/AbstractJCacheConfiguration.class
在使用Spring-MVC环境时 报错: Failed to parse configuration class [org.springframework.cache.aspectj.AspectJ ...
- java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
在使用Spring框架时 报错 :java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource 原因:为引入spring-a ...
- phpcms前台无法退出无法清除cookie
phpcms前台无法退出无法清除cookie 如果caches\configs\system.php 中的cookie_path 设置为空..就会出现退不出的情况..设置为'cookie_path' ...
- vim复制粘贴导致多行出现#号解决办法
在vim内复制多行假如复制的行带有#号会导致其他不带#号的行自动加# 解决办法,输入一下命令再粘贴即可 :set paste
- Data - 数据挖掘的基础概念
主要内容来自于<微信公众号:程SIR说> 1 数据挖掘 数据挖掘(Data Mining,简称DM),是指从大量的数据中,挖掘出未知的且有价值的信息和知识的过程. 数据挖掘是一门交叉学科, ...