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 called an integer sequence of length N if all its elements A1 A2 .. AN are non-negative integers less than 2 000 000 000. Consider two integer sequences of length N, A and X. The result of their multiplication (A*X) is an integer number R=A1*X1 + A2*X2 + .. + AN*XN. Your task is to solve the equation A*X=B (mod P), given the integer sequence A and the integer numbers B and P.
Input
The first line contains the integer numbers N (1<=N<=100) - the length of the integer sequences - P (1<=P<=10 000) and B (0<=B<=P-1). The second line contains the elements of the sequence A, separated by blanks: A1 A2 .. AN.
Output
You should print one line containing the word "YES" if there exists at least one integer sequence X which is a solution to the equation, or print "NO" otherwise. If the answer is "YES", the next line should contain N non-negative integers separated by blanks: X1 X2 .. XN.
Sample Input #1
2 7 4
7 3
Sample Output #1
YES
0 6
Sample Input #2
3 10 1
2 4 6
Sample Output #2
NO
线性同余方程,不断使前k个项余p得到最大公约数,同除去最大公约数,逆推即可
#include <cstdio>
using namespace std;
int extgcd(int a,int b,int& x,int& y){
if(a==0){
x=0;y=1;
return b;
}
int t=extgcd(b%a,a,x,y);
int temp=x;
x=y-b/a*x;
y=temp;
return t;
}
int num[110],x[110],y[110];
int main(){
int cgcd,n,p,b;
scanf("%d%d%d",&n,&p,&b);
for(int i=0;i<n;i++){
scanf("%d",num+i);
num[i]%=p;
}
cgcd=num[0];
for(int i=1;i<n;i++){
cgcd=extgcd(cgcd,num[i],x[i],y[i]);
}
cgcd=extgcd(cgcd,p,x[n],y[n]);
if(b%cgcd!=0)puts("NO");
else {
puts("YES");
b/=cgcd;
y[0]=1;
for(int i=n-1;i>=0;i--){
while(x[i+1]<0)x[i+1]+=p;
b*=x[i+1];
b%=p;
while(y[i]<0)y[i]+=p;
y[i]=y[i]*b%p;
}
for(int i=0;i<n;i++){
printf("%d%c",y[i],i==n-1?'\n':' ');
}
}
return 0;
}
SGU 140. Integer Sequences 线性同余,数论 难度:2的更多相关文章
- sgu 137. Funny Strings 线性同余,数论,构造 难度:3
137. Funny Strings time limit per test: 0.25 sec. memory limit per test: 4096 KB Let's consider a st ...
- POJ 3087 Shuffle'm Up 线性同余,暴力 难度:2
http://poj.org/problem?id=3087 设:s1={A1,A2,A3,...Ac} s2={Ac+1,Ac+2,Ac+3,....A2c} 则 合在一起成为 Ac+1,A1,Ac ...
- POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2
http://poj.org/problem?id=1426 测试了一番,从1-200的所有值都有long long下的解,所以可以直接用long long 存储 从1出发,每次向10*s和10*s+ ...
- 解密随机数生成器(二)——从java源码看线性同余算法
Random Java中的Random类生成的是伪随机数,使用的是48-bit的种子,然后调用一个linear congruential formula线性同余方程(Donald Knuth的编程艺术 ...
- python3 线性同余发生器 ( random 随机数生成器 ) 伪随机数产生周期的一些探究
import random x=[str(random.randint(0, 5)) for i in range(10)] x_str=''.join(x) y=[str(random.randin ...
- K - Large Division 判断a是否是b的倍数。 a (-10^200 ≤ a ≤ 10^200) and b (|b| > 0, b fits into a 32 bit signed integer). 思路:取余;
/** 题目:K - Large Division 链接:https://vjudge.net/contest/154246#problem/K 题意:判断a是否是b的倍数. a (-10^200 ≤ ...
- Finite Encyclopedia of Integer Sequences(找规律)
6617: Finite Encyclopedia of Integer Sequences 时间限制: 1 Sec 内存限制: 128 MB提交: 375 解决: 91[提交] [状态] [讨论 ...
- ACM程序设计选修课——1043: Radical loves integer sequences(YY)
1043: Radical loves integer sequences Time Limit: 1 Sec Memory Limit: 128 MB Submit: 36 Solved: 4 ...
- hdu1573:数论,线性同余方程组
题目大意: 给定一个N ,m 找到小于N的 对于i=1....m,满足 x mod ai=bi 的 x 的数量. 分析 先求出 同余方程组 的最小解x0,然后 每增加lcm(a1...,am)都 ...
随机推荐
- Python开发【项目】:博客后台
概述 通过自己写的博客后台代码.思路,来与武sir的代码进行一个差异化的比较,记录之间的差距,改善以后写代码的思路 博客后台这个项目,对之前Django学习的各个知识点都有涉及到,非常重要 用户登录验 ...
- Mybatis三剑客之mybatis-plugin
搜索mybatis plugin并安装. 如果没有的话,就按照如下: 1. 简介 mybatis plugin作为一款优秀的mybatis跳转插件,比起free mybatis plugin插 ...
- visio基础教程(一)
- 浏览器输入url的全过程
########################################################################### ######################## ...
- SpringBoot 通过自定义注解实现AOP切面编程实例
一直心心念的想写一篇关于AOP切面实例的博文,拖更了许久之后,今天终于着手下笔将其完成. 基础概念 1.切面(Aspect) 首先要理解‘切’字,需要把对象想象成一个立方体,传统的面向对象变成思维,类 ...
- SQL case when 多条件查询
基于列的逻辑表达式,其实就是CASE表达式.可以用在SELECT,UPDATE,DELETE,SET以及IN,WHERE,ORDER BY和HAVING子句之后.下面给个简单示例:
- window.location.href = basePath + "paper/deleteExpertComment.action?expertId="+$(this).prev().val();
window.location.href = basePath + "paper/deleteExpertComment.action?expertId="+$(this).pre ...
- java压缩zip文件中文乱码问题
用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问题网上很多人给出了解决方法,两种:修改sun的源码:使用开源的类库org.apache.tools.zip.Zip ...
- Jquery的深度拷贝和深度克隆
有人问,拷贝和克隆不都是“复制”的意思吗. 这位看官问的好,一般情况下是一样的,但在jquery中却有些不同.jqurey深度拷贝一般只js对象的复制,是$.extend()方法,jquery深度克隆 ...
- A*寻路算法详细解读
文章目录 A*算法描述 简化搜索区域 概述算法步骤 进一步解释 具体寻路过程 模拟需要更新F值的情况 Lua代码实现 在学习A*算法之前,很好奇的是A*为什么叫做A*.在知乎上找到一个回答,大致意思是 ...