codeforce453DIV2——D. GCD of Polynomials
题意
给出n(1–150).
输出两个多项式A,B从常数到最高次的系数,使得对两个多项式求gcd时,恰好经过n步得到结果.
多项式的gcd一步是指(A(x),B(x))变成(B,A mod B)的过程,且当A mod B为0时,视为得到结果B.
A mod B为多项式求余,参见 long division.
要求两个多项式的所有系数都是1,0,-1.前导系数(最高次项系数)为1,度数(最高次)不超过n,第一个多项式的度数大于第二个
分析:
这个题懵逼了好几天,题解愣是没看懂,来学校后在宿舍用笔划拉了好久终于大概是了解了是怎么回事了。
逆推一下GCD的公式:GCD(A,B)=GCD(AX+B,A)。所以可以得到An=An-1*X+An-2。但是题目中有个限制每一位的系数的绝对值不大于1。将GCD变形可以得到GCD(AX+B,A)=GCD(AX-B,A),所以在递推的时候如果系数超过1,那么相减,否则相加。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream> using namespace std;
const int maxn=+;
int a[maxn][maxn];
int n; int main(){
scanf("%d",&n);
a[][]=;a[][]=;
for(int i=;i<=n;i++){
for(int j=;j<=i;j++){
if(j>)
a[i][j]=a[i-][j-];
if(j<=i-){
if(a[i][j]+a[i-][j]>)
a[i][j]-=a[i-][j];
else
a[i][j]+=a[i-][j];
}
}
}
printf("%d\n",n);
for(int i=;i<=n;i++){
printf("%d ",a[n][i]);
}
printf("\n");
printf("%d\n",n-);
for(int i=;i<n;i++){
printf("%d ",a[n-][i]);
}
return ;
}
codeforce453DIV2——D. GCD of Polynomials的更多相关文章
- Codeforces D - GCD of Polynomials
D - GCD of Polynomials 逆推,根据(i-2)次多项f(i-2)式和(i-1)次多项式f(i-1)推出i次多项式f(i) f(i)=f(i-1)*x+f(i-2) 样例已经给出0次 ...
- 【CodeForces】901 B. GCD of Polynomials
[题目]B. GCD of Polynomials [题意]给定n,要求两个最高次项不超过n的多项式(第一个>第二个),使得到它们GCD的辗转次数为n.n<=150. [算法]构造 [题解 ...
- Codeforces 902D/901B - GCD of Polynomials
传送门:http://codeforces.com/contest/902/problem/D 本题是一个数学问题——多项式整除. 对于两个整数a.b,求最大公约数gcd(a,b)的辗转相除法的函数如 ...
- Algebraic Kernel ( Arithmetic and Algebra) CGAL 4.13 -User Manual
1 Introduction Real solving of polynomials is a fundamental problem with a wide application range. T ...
- Polynomial ( Arithmetic and Algebra) CGAL 4.13 -User Manual
1 Fundamentals A polynomial is either zero, or can be written as the sum of one or more non-zero ter ...
- Codeforces Round #453 (Div. 1)
Codeforces Round #453 (Div. 1) A. Hashing Trees 题目描述:给出一棵树的高度和每一层的节点数,问是否有两棵树都满足这个条件,若有,则输出这两棵树,否则输出 ...
- Codeforces Round #453 ( Div. 2) Editorial ABCD
A. Visiting a Friend time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #453 Div. 2 A B C D (暂时)
// 从大作业和实验报告中爬出来水一发 // 补题...还是得排在写完实验报告之后... A. Visiting a Friend 题意 给定若干段行车区间,问能否从起点到终点 思路 扫描一遍,维护最 ...
- Codeforces Round #453
Visiting a Friend Solution Coloring a Tree 自顶向下 Solution Hashing Trees 连续2层节点数都超过1时能异构 Solution GCD ...
随机推荐
- poj2378(dfs,树形dp)
和poj3107,poj1655一样的方法 #include<iostream> #include<cstdio> #include<cstdlib> #inclu ...
- Build ios app with Delphi Xe4. Lazy Social Talker ready for sale.
Yes, it is build with Delphi XE4. try it. now. What is Lazy Social Talker? Lazy Social Talker is a ...
- Angular在render完成之后,执行Js脚本
AngularJs中,如何在render完成之后,执行Js脚本 app.directive('onFinishRenderFilters', function ($timeout) { return ...
- HDU - 5297:Y sequence (迭代&容斥)
Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hat ...
- SCARA——OpenGL入门学习五六(三维变换、动画)
OpenGL入门学习(五) 此课为三维变换的内容,比较枯燥.主要是因为很多函数在单独使用时都不好描述其效果, 在前面绘制几何图形的时候,大家是否觉得我们绘图的范围太狭隘了呢?坐标只能从-1到1,还只能 ...
- throw、try 和 catch
try 语句允许我们定义在执行时进行错误测试的代码块. catch 语句允许我们定义当 try 代码块发生错误时,所执行的代码块. JavaScript 语句 try 和 catch 是成对出现的. ...
- linux之使用rpmbuild打rpm包
linux之使用rpmbuild打rpm包 前言: 已从事linux运维工作数年,感觉自己还是个小菜鸟,没有大神那么的钻研的精神.只是单纯热爱,喜欢对着黑色的屏幕敲击命令,喜欢这种感觉.为什么要做RP ...
- FPGA中逻辑复制
copy from http://www.cnblogs.com/linjie-swust/archive/2012/03/27/FPGA_verilog.html 在FPGA设计中经常使用到逻辑复制 ...
- expected_conditions 库的使用方法
from selenium.webdriver.support import expected_conditions as EC 例子一: 例子二:(判断元素存在文本"糯米")
- Effective java笔记3--类和接口1
一.使类和成员的可访问能力最小化 要想区别一个设计良好的模块与一个设计不好的模块,最重要的因素是,这个模块对于外部的其他模块而言,是否隐藏了内部的数据和其他的实现细节.一个设计良好的模块会隐藏所有的实 ...