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 if all its elements A1 A2 .. AN are non-negative integers less than 2 000 000 000. Consider two integer sequences of length NA 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2

    http://poj.org/problem?id=1426 测试了一番,从1-200的所有值都有long long下的解,所以可以直接用long long 存储 从1出发,每次向10*s和10*s+ ...

  4. 解密随机数生成器(二)——从java源码看线性同余算法

    Random Java中的Random类生成的是伪随机数,使用的是48-bit的种子,然后调用一个linear congruential formula线性同余方程(Donald Knuth的编程艺术 ...

  5. python3 线性同余发生器 ( random 随机数生成器 ) 伪随机数产生周期的一些探究

    import random x=[str(random.randint(0, 5)) for i in range(10)] x_str=''.join(x) y=[str(random.randin ...

  6. 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 ≤ ...

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

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

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

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

  9. hdu1573:数论,线性同余方程组

    题目大意: 给定一个N ,m 找到小于N的  对于i=1....m,满足  x mod ai=bi  的 x 的数量. 分析 先求出 同余方程组 的最小解x0,然后 每增加lcm(a1...,am)都 ...

随机推荐

  1. talib 中文文档(十五):Math Operator Functions 数学方法

    Math Operator Functions 数学运算符函数 ADD - Vector Arithmetic Add 函数名:ADD 名称:向量加法运算 real = ADD(high, low) ...

  2. Linux squid 缓存服务器

    一.简介 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用 ...

  3. 11.Git分支管理

    分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ...

  4. Gitlab汉化为中文版

    查看当前的gitlab版本号 cat /opt/gitlab/embedded/service/gitlab-rails/VERSION 11.1.4 打开这个网址:https://gitlab.co ...

  5. docker——核心实现技术

    作为一种容器虚拟化技术,Docker深度应用了操作系统的多项底层支持技术. 早期版本的Docker是基于已经成熟的Linux Container(LXC)技术实现的.自从0.9版本起,Docker逐渐 ...

  6. spring AOP的注解实例

    上一篇写了spring AOP 的两种代理,这里开始AOP的实现了,个人喜欢用注解方式,原因是相对于XML方式注解方式更灵活,更强大,更可扩展.所以XML方式的AOP实现就被我抛弃了. 实现Sprin ...

  7. animation CSS3动画总结

    最近一个小游戏项目用到了CSS3的动画属性,例如transition.transform.animation.经过三个星期,终于做完了,利用周末好好梳理总结一下. keyframes这个属性用来定义一 ...

  8. 【小坑】java下载excel文件

    excel文件的导入导出是很常见的功能,这次做了个下载的功能,踩了一些坑,记下来避免以后重复踩…… 1.inputstream序列化问题 Could not write JSON document: ...

  9. 常用php操作redis命令整理(四)SET类型

    SADD 将一个或多个member元素加入到集合key当中.(从左侧插入,最后插入的元素在0位置),集合中已经存在TK 则返回false,不存在添加成功 返回true <?php var_dum ...

  10. poj3071 Football(概率dp)

    poj3071 Football 题意:有2^n支球队比赛,每次和相邻的球队踢,两两淘汰,给定任意两支球队相互踢赢的概率,求最后哪只球队最可能夺冠. 我们可以十分显然(大雾)地列出转移方程(设$f[ ...