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. Jenkins分布式与并行

    目录 一.简介 二.agent 通过JNLP协议增加agent 通过Swarm插件增加agent agent部分详解 三.agent放入Docker 使用Docker 配置Docker私有仓库 四.并 ...

  2. 时间同步——TSN协议802.1AS介绍

    前言之前的主题TSN的发展历史和协议族现状介绍了TSN技术的缘起,最近一期的主题TSN协议导读从定时与同步.延时.可靠性.资源管理四个方面,帮助大家了解TSN协议族包含哪些子协议,以及这些子协议的作用 ...

  3. 30个类手写Spring核心原理之自定义ORM(上)(6)

    本文节选自<Spring 5核心原理> 1 实现思路概述 1.1 从ResultSet说起 说到ResultSet,有Java开发经验的"小伙伴"自然最熟悉不过了,不过 ...

  4. box-shadow(盒子阴影)

    box-shadow 属性可以设置一个或多个下拉阴影的框 可以在同一个元素上设置多个阴影效果,并用逗号将他们分隔开.该属性可设置的值包括阴影的X轴偏移量.Y轴偏移量.模糊半径.扩散半径和颜色. 语法: ...

  5. CMAKE为项目输出名添加后缀

    概述 本文将介绍cmake配置动态库和可执行程序两种,使用cmake为其添加Debug和Release配置下的后缀 动态库 下面将介绍使用CMAKE_DEBUG_POSTFIX和CMAKE_RELEA ...

  6. c++设计模式概述之备忘录

    代买写的不够规范,,目的是缩短篇幅,实际中请不要这样做. 1.概述 和这个模式相似的生活场景,比如 office的撤销操作.VS  和 xcode等IDE的撤销操作 . 其实都是恢复到上一个或者下一个 ...

  7. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  8. Docker 与 K8S学习笔记(三)—— 镜像的使用

    前面的文章介绍过镜像的三种获取方式: 下载并使用别人创建好的镜像: 在现有镜像上创建新的镜像: 从无到有创建镜像. 本文主要介绍前两种. 一.下载镜像 在Docker Hub上有大量优质镜像可以使用, ...

  9. 「实用」打造自我感觉非常漂亮的Mac终端

    背景 (今天我是一个美妆博主) 突然发现自己使用的iterm2终端样式有些朴素,为了让她看起来花枝招展的,我决定给她打扮打扮.毕竟每天面对她的时间比对象还多-- 效果对比 因为每个人的喜好都不一样,所 ...

  10. 【机器学习】matplotlib库练习-函数绘图

    # 1创建2个图形区域,一个叫做green,大小是16,8,一个叫做red,大小是10,6 # 2绿色区域画一条绿色的正弦曲线,红色区域化两条线,一条是绿色的正弦曲线,一条是红色的余弦曲线 # 3在g ...