同余方程组:

  先来看一道题目:有物不知其数,三三数之剩二;五五数之剩三;七七数之剩二。问物几何?  然后我们可以做如下变换,设x为所求的数。

   x%3=2              x ≡ a1(%m1)  ①
   x%5=3  ===>  x ≡ a2(%m2)  ②
   x%7=2              x ≡ a3(%m3)

  根据前面两式可以得到

    x = a1+m1y1     (1)
   x = a2+m2y2

  两式相减得到  m1y1 - m2y2 = a2 - a1  这是一个线性不定方程,可解出y1  ---> linearEquation(m1,-m2,a2-a1)   带回(1),得特解x0 = a1+m1*y1 --> 得到通解表达式 x =x0 + k*lcm(m1,m2) 得一个新方程 x = x0 (mod lcm(m1,m2))

  代码:

/**
*
* @param a 余数组成的数组
* @param m 模组成的数组
* @return
* @throws Exception
*/
public static long linearEquationGroup(Long[] a, Long[] m) throws Exception {
int len = a.length;
if (len == 0 && a[0] == 0)
return m[0]; for (int i = 1; i < len; i++) {
// 这里往前看是两个方程
long a2_a1 = a[i] - a[i - 1];
long d = linearEquation(m[i - 1], -m[i], a2_a1);
// 现在的x是y1,用y1求得一个特解
long x0 = a[i - 1] + m[i - 1] * x;
long lcm = m[i - 1] * m[i] / d;
a[i] = (x0 % lcm + lcm) % lcm;// x0变成正数
m[i] = lcm;
}
// 合并完之后,只有一个方程 : x = a[len-1] (% m[len-1])
//long d = linearEquation(1, m[len-1], a[len-1]);
return a[len - 1] % m[len - 1];
}

题目:POJ1006 生理周期

  

  

  代码:

 import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; public class POJ1006 { public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int t = 1;
List<Long[]> aList = new ArrayList<Long[]>();
List<Long> dList = new ArrayList<Long>();
while(scanner.hasNext()){
Long []a = {scanner.nextLong(),scanner.nextLong(),scanner.nextLong()};
Long d = scanner.nextLong();
if (a[0]==-1&&a[1]==-1&&a[2]==-1&&d==-1) {
break;
}else {
aList.add(a);
dList.add(d);
}
}
for (int i = 0; i < aList.size(); i++) {
Long[] a = aList.get(i);
long d = dList.get(i);
Long[] m = { (long) 23, (long) 28, (long) 33 };
long res = MyGcd.linearEquationGroup(a, m);
while (res <= d) {
res += 21252;
}
System.out.println("Case " + (t++) + ": the next triple peak occurs in " + (res - d) + " days.");
}
} private static class MyGcd {
static long x;
static long y; /**
*
* @param a 余数组成的数组
* @param m 模组成的数组
* @return
* @throws Exception
*/
public static long linearEquationGroup(Long[] a, Long[] m) throws Exception {
int len = a.length;
if (len == 0 && a[0] == 0)
return m[0]; for (int i = 1; i < len; i++) {
// 这里往前看是两个方程
long a2_a1 = a[i] - a[i - 1];
long d = linearEquation(m[i - 1], -m[i], a2_a1);
// 现在的x是y1,用y1求得一个特解
long x0 = a[i - 1] + m[i - 1] * x;
long lcm = m[i - 1] * m[i] / d;
a[i] = (x0 % lcm + lcm) % lcm;// x0变成正数
m[i] = lcm;
}
// 合并完之后,只有一个方程 : x = a[len-1] (% m[len-1])
//long d = linearEquation(1, m[len-1], a[len-1]);
return a[len - 1] % m[len - 1];
} public static long inverseElement(long a, long mo) throws Exception { long d = linearEquation(a, mo, 1);
x = (x % mo + mo) % mo;
return d;
} public static long linearEquation(long a, long b, long m) throws Exception {
long d = ext_gcd(a, b);
// m不是gcd(a,b)的倍数,这个方程无解
if (m % d != 0)
throw new Exception("无解");
long n = m / d;// 约一下,考虑m是d的倍数
x *= n;
y *= n;
return d;
} public static long ext_gcd(long a, long b) { if (b == 0) {
x = 1;
y = 0;
return a;
}
long res = ext_gcd(b, a % b);
long x1 = x;// 备份x
x = y;// 更新x
y = x1 - a / b * y;// 更新y
return res;
} } }

  结果:

    

【同余方程组】POJ1006 生理周期的更多相关文章

  1. poj1006 生理周期

    生理周期 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 138947   Accepted: 44597 Descripti ...

  2. 【数论】【中国剩余定理】poj1006 生理周期

    CRT用于求解一元线性同余方程组(模数互质),实际上模数不互质我们也可以解决,在之前的某篇文章里提过.如下 http://www.cnblogs.com/autsky-jadek/p/6596010. ...

  3. poj1006生理周期(中国剩余定理)

    生理周期 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 139224   Accepted: 44687 Descripti ...

  4. poj1006生理周期(中国剩余定理)

    /* 中国剩余定理可以描述为: 若某数x分别被d1..….dn除得的余数为r1.r2.….rn,则可表示为下式: x=R1r1+R2r2+…+Rnrn+RD 其中R1是d2.d3.….dn的公倍数,而 ...

  5. [POJ1006]生理周期 (中国剩余定理)

    蒟蒻并不会中国剩余定理 交的时候还出现了PE的错误 下面是AC代码 #include<iostream> #include<cstdio> using namespace st ...

  6. OpenJ_Bailian 4148 生理周期

    生理周期 OpenJ_Bailian - 4148 Time limit1000 ms Memory limit65536 kB OS Linux SourceEast Central North A ...

  7. 生理周期[PKU1006]

    生理周期 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 132195   Accepted: 42171 Descripti ...

  8. 生理周期(c++实现)

    描述:人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23 天. 28 天和33 天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰,人会思维 ...

  9. ACM第二题 生理周期

    人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰,人会思维敏捷,精力容易 ...

随机推荐

  1. org.json.JSONObject的getString和optString使用注意事项

    结论:org.json.JSONObject的getString如果取不到对应的key会抛出异常,optString则不会 /** * Returns the value mapped by {@co ...

  2. 获取clock ticks per second

    #include <sys/syscall.h> #include <stdio.h> #include <unistd.h> int main() { print ...

  3. ISP PIPLINE (十四) AE(自动曝光)

    自动曝光可以可以通过调节 模拟增益,数字增益,曝光时间,光圈大小来调剂曝光. 曝光在ISP PIPLINE的位置. (先介绍一个额外的知识点: ) gamma compression(也就是de-ga ...

  4. 解决win10搜索框不能使用的问题

    1.首先,打开管理员命令窗口,win+x,可以看到弹出一个窗口,打开windows Powershell(管理员) 2,输入 Get-AppXPackage -Name Microsoft.Windo ...

  5. CentOS,crontab的学习、使用、问题解决记录

    参考:http://blog.csdn.net/luanwpp/article/details/7490871 参考: http://mp.weixin.qq.com/s?src=11&tim ...

  6. css实现中文换行,英文换行,超出省略

    英文换行时,是以单词换行,在对应的标签添加对应的属性即可 1 word-break:break-all;只对英文起作用,以字母作为换行依据 2 word-wrap:break-word; 只对英文起作 ...

  7. Spark累加器(Accumulator)陷阱及解决办法

    累加器(accumulator)是Spark中提供的一种分布式的变量机制,其原理类似于mapreduce,即分布式的改变,然后聚合这些改变.累加器的一个常见用途是在调试时对作业执行过程中的事件进行计数 ...

  8. 推荐一些iOS博客

    公司性质的: 公司 地址 美团 http://tech.meituan.com/archives 个人博客: 博主 地址 (斜体的技术文章较少) 王巍(onevcat) https://onevcat ...

  9. .NET题目(收集来自网络)

    1: .NET和c#有什么区别? 答: .NET一般是指.NET FrameWork框架,是一种平台,一种技术 c#是一种编程语言,是可以基于.NET平台的应用 2: c#中的委托是什么?事件是不是一 ...

  10. Mysql 常见问题

    ------------------------------------------------ ------------------------------------------------ -- ...