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】两个for循环嵌套练习
要求:取下方篮框中内容并写入csv文件中.用两个for循环将每一行取出来作为一个元素存放到新数组中. # coding=utf-8 from selenium import webdriver fro ...
- T-SQL练习题
转自:http://www.cnblogs.com/jenrrychen/p/5348546.html 1 - 3 题: 数据表结构: OrderID ProductID OrderDate Sal ...
- Java面试人事篇(二)
1.请你自我介绍一下你自己? 回答提示:一般人回答这个问题过于平常,只说姓名.年龄.爱好.工作经验,这些在简历上都有.其实,企业最希望知道的是求职者能否胜任工作,包括:最强的技能.最深入研究的知识领域 ...
- 007-spring cache-缓存实现-02-springboot ehcahe3实现、springboot caffeine实现
一.springboot ehcahe3实现步骤 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认CacheProvider.Ehcache是一种广泛 ...
- sql 用xml方式插入数据乱码问题解决方法
sql 使用存储过程 参数为xml字符串 xml不要写编码,如下 <?xml version=\"1.0\" ?><root>数据字符串</root& ...
- [WorldWind学习]20.修改ShapeFileLayer类及托管D3D文字绘制方法
PluginSDK\ShapeFileLayer.cs Line:1027char[] fieldDataChars = dbfReader.ReadChars(fieldHeaders[j].Fie ...
- sdut AOE网上的关键路径(spfa+前向星)
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2498&cid=1304 题目描述 一个无环的有向图称为无环图(Directed Acyc ...
- (转)Terraform,自动化配置与编排必备利器
本文来自作者 QingCloud实践课堂 在 GitChat 上分享 「Terraform,自动化配置与编排必备利器」 Terraform - Infrastructure as Code 什么是 T ...
- 转:CentOS设置程序开机自启动的方法
转自:http://www.centos.bz/2011/09/centos-setup-process-startup-boot/ 1.把启动程序的命令添加到/etc/rc.d/rc.local文件 ...
- VS2010/MFC编程入门之十(对话框:设置对话框控件的Tab顺序)
前面几节鸡啄米为大家演示了加法计算器程序完整的编写过程,本节主要讲对话框上控件的Tab顺序如何调整. 上一讲为“计算”按钮添加了消息处理函数后,加法计算器已经能够进行浮点数的加法运算.但是还有个遗留的 ...