Codeforces 902D/901B - GCD of Polynomials
传送门:http://codeforces.com/contest/902/problem/D
本题是一个数学问题——多项式整除。
对于两个整数a、b,求最大公约数gcd(a,b)的辗转相除法的函数如下:
int gcd(int a, int b) {
if (b == ) return a;
return gcd(b, a % b);
}
一次辗转相除法将数对(a,b)转化成(b,a mod b),直至b=0。
对于多项式A(x),定义deg A(x)为多项式的次数。对于多项式A、B,定义多项式mod运算:若A(x)=B(x)·D(x)+R(x),deg R(x)<deg B(x),则R(x)≡A(x) mod B(x)。
于是,一次辗转相除将多项式对(A,B)转化成(B,A mod B),直至B=0。
已知两个多项式A(x)、B(x)的系数在{-1,0,1}中取值,且deg A(x)≥deg B(x);构成的多项式对(A,B),经过n次辗转相除后结束操作。求可能的多项式A(x)、B(x)。
考虑整数的情形:Fibonacci数列:F(0)=1,F(1)=1,F(n+1)=F(n)+F(n-1)。
于是,一次辗转相除法将(F(n+1),F(n))转化成(F(n),F(n-1))。于是,(F(n),F(n-1))在n次辗转相除后结束操作。
类似地,构造多项式的情形:P(0)=1,P(1)=x,P(n+1)=x·P(n)±P(n-1)。
递推时,维护P(n+1)的系数在{-1,0,1}中取值,这可以通过符号选择(+/-)实现。
还可以构造多项式:P(0)=1,P(1)=x,P(n+1)≡x·P(n)+P(n-1) mod2。
参考程序如下:
#include <stdio.h>
#define MAX_N 200 int p[MAX_N][MAX_N]; int main(void)
{
int n;
scanf("%d", &n);
p[][] = ;
p[][] = ;
for (int i = ; i < n; i++) {
//p[i + 1] = {x * p[i] + p[i - 1]} % 2;
for (int j = ; j <= i; j++)
p[i + ][j + ] += p[i][j];
for (int j = ; j <= i - ; j++)
p[i + ][j] += p[i - ][j];
for (int j = ; j <= i + ; j++)
p[i + ][j] %= ;
}
int deg;
for (deg = n; p[n][deg] == ; deg--);
printf("%d\n", deg);
for (int i = ; i <= deg; i++)
printf("%d ", p[n][i]);
printf("\n");
for (deg = n - ; p[n - ][deg] == ; deg--);
printf("%d\n", deg);
for (int i = ; i <= deg; i++)
printf("%d ", p[n - ][i]);
printf("\n");
return ;
}
Codeforces 902D/901B - 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 803C Maximal GCD(GCD数学)
Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...
- Codeforces 803C. Maximal GCD 二分
C. Maximal GCD time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...
- Codeforces 338 D. GCD Table
http://codeforces.com/problemset/problem/338/D 题意: 有一张n*m的表格,其中第i行第j列的数为gcd(i,j) 给出k个数 问在这张表格中是否 有某一 ...
- Codeforces 583 DIV2 GCD Table 贪心
原题链接:http://codeforces.com/problemset/problem/583/C 题意: 大概就是给你个gcd表,让你还原整个序列. 题解: 由$GCD(a,a)=a$,我们知道 ...
- [Codeforces Round511C] Enlarge GCD
[题目链接] https://codeforces.com/contest/1047/problem/C [算法] 首先求出n个数的最大公约数g , 将每个数除以g , 那么 , 问题就转化为在n个数 ...
- Codeforces 803C. Maximal GCD
题目链接:http://codeforces.com/contest/803/problem/C 中了若干trick之后才过... k个数的严格递增序列最小权值和就是${n*(n+1)/2}$,枚举这 ...
- CodeForces - 1101D:GCD Counting (树分治)
You are given a tree consisting of n vertices. A number is written on each vertex; the number on ver ...
随机推荐
- erlang Unicode 处理
最近在使用erlang做游戏服务器,而字符串在服务器编程中的地位是十分重要的,于是便想仔细研究下字符编码,以及erlang下的字符串处理.先从Unicode开始吧.... [Unicode] Unic ...
- LeetCode 28 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 思路:1.先将被除数和除数转化为long的非负 ...
- C# WinForm小程序(技术改变世界-cnblog)
WinForm小程序(技术改变世界-cnblog) 需求: 1.点击按钮 更新 当前时间 2.输入 身份证,必须身份证 排序(类似银行卡那样的空格),自动生成空格排序 3.实现 必须按 第一个按 ...
- 【POJ 1964】 City Game
[题目链接] http://poj.org/problem?id=1964 [算法] 记f[i]表示第i行最多向上延伸的行数 然后,对于每一行,我们用单调栈计算出这一行向上延伸的最大矩形面积,取最大值 ...
- P4135 作诗——分块
题目:https://www.luogu.org/problemnew/show/P4135 分块大法: 块之间记录答案,每一块记录次数前缀和: 注意每次把桶中需要用到位置赋值就好了: 为什么加了特判 ...
- vim gvim技巧大全(9)(转载)
vim gvim技巧大全(9) 2 用命令}移动到这个段落的底部,标记为b3 输入命令:'a,'b move来移动文本.老版本的Vi编辑器不能很好的来处理多文件.但是Vim在处理多文件上却显得优秀得多 ...
- SqlMap常用参数(一)
sqlmap可谓是利用sql注入的神器了,sqlmap的参数很多,接下介绍几种常见的参数. 一.注入access数据库常用的参数 sqlmap.py -u "url" //判断参 ...
- C - Gravity Flip
Problem description Little Chris is bored during his physics lessons (too easy), so he has built a t ...
- Spring Boot (2) Restful风格接口
Rest接口 动态页面jsp早已过时,现在流行的是vuejs.angularjs.react等前端框架 调用 rest接口(json格式),如果是单台服务器,用动态还是静态页面可能没什么大区别,如果服 ...
- struts2标签(五)
标签体系结构 jsp出现目的是为了取代servlet,结果逻辑代码,数据库代码都放到了jsp页面中. 为了解决jsp中代码过多的问题,struts2标签分为普通标签和UI标签. 使用struts2标签 ...