https://codeforces.com/problemsets/acmsguru/problem/99999/140

n元同余方程的求解

对于任意二元我们可以替换成kgcd(a,b),不断迭代下去最后会变成ax=b(mod p)的情况,然后,再返回去求解,得到g,然后后面变成不等式求解,直接将y[i-1]*g即可,但是要最小整数解。过程当中不断的用p取模,这样可以保证结果不会很大。因为只要一个结果,通项公式为b/d的一个组合,小的约束小,大的约束大, 大的一定包含在小的里面,那么大的一定是其中的一个解,所以mod p,但这并不是最小解,而是一个整数解。

 1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 ll ex_gcd(ll a,ll b,ll &x,ll &y)
5 {
6 if(!b){x=1,y=0;return a;}
7 ll d=ex_gcd(b,a%b,x,y);
8 ll tmp=x;
9 x=y;y=tmp-a/b*y;
10 return d;
11 }
12 const ll N=520;
13 ll n,m[N],p,b,x[N],y[N],ans[N];
14 int main()
15 {
16 scanf("%lld%lld%lld",&n,&p,&b);
17 for(int i=1;i<=n;i++) scanf("%lld",&m[i]),m[i]%=p;
18 ll g=m[1];m[n+1]=p;
19 for(int i=1;i<=n;i++) g=ex_gcd(g,m[i+1],x[i],y[i]);
20 if(b%g) printf("NO\n");
21 else
22 {
23 printf("YES\n");
24 g=b/g,y[0]=1;
25 for(int i=n;i>=1;i--)
26 {
27 g=(g*x[i]%p+p)%p;
28 ans[i]=(g*y[i-1]%p+p)%p;
29 }
30 for(int i=1;i<=n;i++) printf("%lld%c",ans[i],(i==n?'\n':' '));
31 }
32 return 0;
33 }

SGU140. Integer Sequences的更多相关文章

  1. SGU 140. Integer Sequences 线性同余,数论 难度:2

    140. Integer Sequences time limit per test: 0.25 sec. memory limit per test: 4096 KB A sequence A is ...

  2. Finite Encyclopedia of Integer Sequences(找规律)

    6617: Finite Encyclopedia of Integer Sequences 时间限制: 1 Sec  内存限制: 128 MB提交: 375  解决: 91[提交] [状态] [讨论 ...

  3. ACM程序设计选修课——1043: Radical loves integer sequences(YY)

    1043: Radical loves integer sequences Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 36  Solved: 4 ...

  4. Codeforces #550 (Div3) - G.Two Merged Sequences(dp / 贪心)

    Problem  Codeforces #550 (Div3) - G.Two Merged Sequences Time Limit: 2000 mSec Problem Description T ...

  5. [Swift]LeetCode801. 使序列递增的最小交换次数 | Minimum Swaps To Make Sequences Increasing

    We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...

  6. [LeetCode] Minimum Swaps To Make Sequences Increasing 使得序列递增的最小交换

    We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...

  7. 801. Minimum Swaps To Make Sequences Increasing

    We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...

  8. 801. Minimum Swaps To Make Sequences Increasing 为使两个数组严格递增,所需要的最小交换次数

    [抄题]: We have two integer sequences A and B of the same non-zero length. We are allowed to swap elem ...

  9. LeetCode 801. Minimum Swaps To Make Sequences Increasing

    原题链接在这里:https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/ 题目: We have two in ...

随机推荐

  1. SOUI3界面编辑器使用说明

    SOUI一直没有官方的界面编辑器,关键是我自己一直坚持手写界面更好控制. 大概是2年前,网友"指尖"开发了一个SOUI2的编辑器,功能非常多,特点是可以拖动控件来实现可视化布局. ...

  2. HTTP隧道解决的问题

    转自别人的文章:https://blog.csdn.net/gogzf/article/details/78385506 客户端通常会用 Web 代理服务器代表它们来访问 Web 服务器.比如,很多公 ...

  3. JS 双向数据绑定、单项数据绑定

    简单的双向数据绑定 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. 去除爬虫采集到的\xa0、\u3000等字符

    \xa0表示不间断空白符,爬虫中遇到它的概率不可谓不小,而经常和它一同出现的还有\u3000.\u2800.\t等Unicode字符串.单从对\xa0.\t.\u3000等含空白字符的处理来说,有以下 ...

  5. 突出显示(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 当一个大的项目文件做好以后,查看全部内容,肉眼多少会有点吃不消,这时就需要"划重点".在Porect里 ...

  6. JS设置网站所有字体变为繁体字

    引入chinese.js var zh_default='n';var zh_choose='t';var zh_expires=7;var zh_class='zh_click';var zh_st ...

  7. cmake配置项目引用动态库

    note 本文将介绍使用FIND_PACKAGE配置项目动态库的方法 cmake version: 3.18 platform: win10 20H2 概述 创建了一个动态库,再由主项目调用该动态库. ...

  8. 【LeetCode】1404. 将二进制表示减到 1 的步骤数 Number of Steps to Reduce a Number in Binary Representation to One

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 转成十进制模拟 修改二进制字符串 日期 题目地址:ht ...

  9. 【LeetCode】762. Prime Number of Set Bits in Binary Representation 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历数字+质数判断 日期 题目地址:https:// ...

  10. 【LeetCode】299. Bulls and Cows 解题报告(Python)

    [LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...