传送门:http://codeforces.com/contest/902/problem/D

本题是一个数学问题——多项式整除。

对于两个整数ab,求最大公约数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)为多项式的次数。对于多项式AB,定义多项式mod运算:若A(x)=B(xD(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)=xP(n+1)=x·P(n)±P(n-1)。

递推时,维护P(n+1)的系数在{-1,0,1}中取值,这可以通过符号选择(+/-)实现。

还可以构造多项式:P(0)=1,P(1)=xP(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的更多相关文章

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

  2. 【CodeForces】901 B. GCD of Polynomials

    [题目]B. GCD of Polynomials [题意]给定n,要求两个最高次项不超过n的多项式(第一个>第二个),使得到它们GCD的辗转次数为n.n<=150. [算法]构造 [题解 ...

  3. codeforces 803C Maximal GCD(GCD数学)

    Maximal GCD 题目链接:http://codeforces.com/contest/803/problem/C 题目大意: 给你n,k(1<=n,k<=1e10). 要你输出k个 ...

  4. Codeforces 803C. Maximal GCD 二分

    C. Maximal GCD time limit per test: 1 second memory limit per test: 256 megabytes input: standard in ...

  5. Codeforces 338 D. GCD Table

    http://codeforces.com/problemset/problem/338/D 题意: 有一张n*m的表格,其中第i行第j列的数为gcd(i,j) 给出k个数 问在这张表格中是否 有某一 ...

  6. Codeforces 583 DIV2 GCD Table 贪心

    原题链接:http://codeforces.com/problemset/problem/583/C 题意: 大概就是给你个gcd表,让你还原整个序列. 题解: 由$GCD(a,a)=a$,我们知道 ...

  7. [Codeforces Round511C] Enlarge GCD

    [题目链接] https://codeforces.com/contest/1047/problem/C [算法] 首先求出n个数的最大公约数g , 将每个数除以g , 那么 , 问题就转化为在n个数 ...

  8. Codeforces 803C. Maximal GCD

    题目链接:http://codeforces.com/contest/803/problem/C 中了若干trick之后才过... k个数的严格递增序列最小权值和就是${n*(n+1)/2}$,枚举这 ...

  9. CodeForces - 1101D:GCD Counting (树分治)

    You are given a tree consisting of n vertices. A number is written on each vertex; the number on ver ...

随机推荐

  1. 使用Linq 查询数据 构建对象 select new{}

    linq 查询数据 /// <summary> /// 汽车品牌及车型 /// </summary> /// <returns></returns> p ...

  2. Lightoj 1235 - Coin Change (IV) 【二分】

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1235 题意: 有N个硬币(N<=18).问是否能在每一个硬币使用不超过两 ...

  3. C++学习笔记22,普通函数重载(1)

    转载请注明出处:http://blog.csdn.net/qq844352155/article/details/31353325 该博文仅用于交流学习,请慎用于不论什么商业用途,本博主保留对该博文的 ...

  4. commons-fileupload上传文件(1)

    近期,写一个上传图片的功能.于是用到commons-fileupload这个组件.提过form提交表单到后台(这里没实用到structs框架).在后台List pl = dfu.parseReques ...

  5. Ubuntu下终端Vim编写C语言程序 AAAAA

    我是开虚拟机下的Ubuntu,装双系统又卸了,Ubuntu默认是不包含编辑器vim和编译器gcc.如果你是刚安装好的Ubuntu电脑,下面我们将来实现自己的第一个程序. 1.准备工作 首先进入root ...

  6. Android WiFi/WiFi热点开发总结

    首先看一下WiFi的自我介绍: Wi-Fi是一种允许电子设备连接到一个无线局域网(WLAN)的技术,通常使用2.4G UHF或5G SHF ISM 射频频段.连接到无线局域网通常是有密码保护的:但也可 ...

  7. codeforces 916E Jamie and Tree dfs序列化+线段树+LCA

    E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...

  8. linux 编译安装TRMPdump(libRTMP)

    需要编译libRTMP,首先需要安装配置编译环境.网上能够找到的资料多是在Windows环境编译.这里介绍一下在Linux系统中编译安装libRTMP,一来给后来者一个参考,二来也给自己做一个备忘录. ...

  9. Python 44 前端概述 、三剑客 、常用标签与分类

    1.前端三剑客是哪三位?文件的后缀内容?在前端开发中的功能是什么? HTML:   .htm .html   内容 CSS:   .css   效果 JS:   .js   行为 2.简述三剑客的主要 ...

  10. 微信公众号开发之文本消息自动回复,以及系统关注自动回复,php代码

    以tshop为例 直接上代码: 企业 cc_wx_sys表为自建,存储系统消息的配置的 字段: id type key status <?php /** * tpshop * ========= ...