一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K。例如,K % 2 = 1, K % 3 = 2, K % 5 = 3。符合条件的最小的K = 23。

 
Input
第1行:1个数N表示后面输入的质数及模的数量。(2 <= N <= 10)
第2 - N + 1行,每行2个数P和M,中间用空格分隔,P是质数,M是K % P的结果。(2 <= P <= 100, 0 <= K < P)
Output
输出符合条件的最小的K。数据中所有K均小于10^9。
Input示例
3
2 1
3 2
5 3
Output示例
23

解:
方法一
 #include <stdio.h>

 typedef struct
{
int p, m;
}str; str s[]; int cmp(const void *a, const void *b)
{
return ((str *)a)->p > ((str *)b)->p ? - : ;
} int main()
{
int n;
while (scanf_s("%d", &n) != EOF)
{
long long mult = ;
int ans;
for (int i = ; i < n; i++)
{
scanf_s("%d%d", &s[i].p, &s[i].m);
mult *= s[i].p;
}
qsort(s, n, sizeof(str), cmp);
for (int i = , flag = ; flag && i * s[].p <= mult; i++)
{
ans = s[].p * i + s[].m;
for (int j = ; ans % s[j].p == s[j].m;j++)
{
if (j == n - )
{
flag = ;
break;
}
}
}
printf("%d\n", ans); }
}

改进法一后的法二:

 #include <stdio.h>

 int main()
{
int n;
while (scanf_s("%d", &n) != EOF)
{
int a, b, c, d;
scanf_s("%d%d", &a, &b);
for (int i = ,j; i < n; i++)
{
scanf_s("%d%d", &c, &d);
for (j = ; (a * j + b) % c != d; j++);
b = a * j + b;
a *= c;
}
printf("%d\n", b);
}
}

法三:

中国剩余定理(https://baike.baidu.com/item/孙子定理/2841597?fromtitle=%E4%B8%AD%E5%9B%BD%E5%89%A9%E4%BD%99%E5%AE%9A%E7%90%86&fromid=11200132&fr=aladdin)+ 拓展欧几里得

(注意中国剩余定理必满足使用拓展欧几里得求逆元的条件,即n个不同质数,其中n-1个的乘积必与剩下的1个互质)

 #include <stdio.h>

 int inv[], pm[];

 void Ex_gcd(int a, int b, int *x, int *y);

 int main()
{
int t;
while (scanf_s("%d", &t) != EOF)
{
long long M = , ans = ;
for (int i = ; i < t; i++)
{
scanf_s("%d%d", &pm[i], &pm[i + ]);
M *= pm[i];
}
for (int i = , temp; i < t; i++)
{
Ex_gcd(M / pm[i], pm[i], &inv[i], &temp);
inv[i] = (inv[i] + pm[i]) % pm[i];
ans = (ans + M / pm[i] * inv[i] * pm[i + ]) % M;
}
printf("%d\n", ans);
}
} void Ex_gcd(int a, int b, int *x, int *y)
{
if (b == )
{
*x = ;
*y = ;
return;
}
Ex_gcd(b, a%b, x, y); //或者可以这么写
int t = *y; //ex_gcd(b, a%b, y, x);
*y = *x - a / b * (*y); //*y = *y - (a / b)**x;
*x = t; //return;
return;
}

(数论)51NOD 1079 中国剩余定理的更多相关文章

  1. acm数论之旅--中国剩余定理

    ACM数论之旅9---中国剩余定理(CRT)(壮哉我大中华╰(*°▽°*)╯)   中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 ...

  2. 51NOD——T 1079 中国剩余定理

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1079 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难 ...

  3. 51 nod 1079 中国剩余定理

    1079 中国剩余定理 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K.例如,K % ...

  4. [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=12083 这道题还是挺耐想的(至少对我来说是这样).开始时我只会60 ...

  5. 数论E - Biorhythms(中国剩余定理,一水)

    E - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  6. hihocode 九十七周 中国剩余定理

    题目1 : 数论六·模线性方程组 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:今天我听到一个挺有意思的故事! 小Hi:什么故事啊? 小Ho:说秦末,刘邦的将军 ...

  7. 《孙子算经》之"物不知数"题:中国剩余定理

    1.<孙子算经>之"物不知数"题 今有物不知其数,三三数之剩二,五五数之剩七,七七数之剩二,问物几何? 2.中国剩余定理 定义: 设 a,b,m 都是整数.  如果 m ...

  8. POJ 1006 中国剩余定理

    #include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...

  9. poj1006中国剩余定理

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103506   Accepted: 31995 Des ...

随机推荐

  1. Ubuntu 12.04 之 LAMP

    搭建LAMP环境 (1)更新软件列表: sudo apt-get update 结果报错: W: 无法下载 bzip2:/var/lib/apt/lists/partial/cn.archive.ub ...

  2. NOIP 2009 潜伏者

    P1071 潜伏者 题目描述 RR 国和 SS 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 SS 国的 RR 国间谍小 CC 终于摸清了 SS 国军用密码的编码规则: ...

  3. dtrace-conf 2016

    https://www.joyent.com/about/events/2016/dtrace-conf

  4. linux 实现VLAN

    本文将在一台linux机器上,利用linuxbridge 等技术模拟创建VLAN 环境. 首先,创建vlan interface ip link add link ens33 name ens33.8 ...

  5. commons-lang常用工具类StringEscapeUtils

    原文:https://my.oschina.net/mousai/blog/88832 在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是为了防止s ...

  6. Spring中JavaConfig特性

    从Spring3開始,增加了JavaConfig特性.JavaConfig特性同意开发人员不必在Spring的xml配置文件里定义bean,能够在Java Class中通过凝视配置bean,假设你讨厌 ...

  7. 南阳OJ独木舟上的旅行

     /*独木舟上的旅行 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描写叙述 进行一次独木舟的旅行活动.独木舟能够在港口租到,而且之间没有差别. 一条独木舟最多仅仅能乘坐 ...

  8. 兔子--CheckBox与Radiobutton的差别

    RadioButton和CheckBox的差别: 1.单个RadioButton在选中后.通过点击无法变为未选中状态,单个CheckBox在选中后.通过点击能够变为未选中. 2.一组RadioButt ...

  9. EXISTS 执行顺序 CLR-2-2-引用类型和值类型

    EXISTS 执行顺序   select * from a where a.s_status=1 and exists (select orderid from b on a.orderid=b.or ...

  10. 系统的BIOS与系统安装

    今天偶尔看到个介绍电脑BIOS的与各种本子安装系统的介绍:(记录一下) 网络地址:http://blog.sina.com.cn/s/blog_4a1faae60102dyek.html