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 ...
随机推荐
- Android自己定义组件系列【1】——自己定义View及ViewGroup
View类是ViewGroup的父类,ViewGroup具有View的全部特性.ViewGroup主要用来充当View的容器.将当中的View作为自己孩子,并对其进行管理.当然孩子也能够是ViewGr ...
- ListView无障碍识别整个listView,不识别item,设置了setContentDescription也没实用
点击ListView的时候.无障碍识别到的是整个listView,不会读点击的那个item. 解决的方法是在getView里手动设置: <span style="font-size:1 ...
- HDU 5533/ 2015长春区域 G.Dancing Stars on Me 暴力
Dancing Stars on Me Problem Description The sky was brushed clean by the wind and the stars were col ...
- luogu1357 花园 状态压缩 矩阵快速幂
题目大意 小L有一座环形花园,沿花园的顺时针方向,他把各个花圃编号为1~N(2<=N<=10^15).他的环形花园每天都会换一个新花样,但他的花园都不外乎一个规则,任意相邻M(2<= ...
- C#操作INI文件(明天陪你看海)
C#操作INI文件 在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据 ...
- 【BZOJ 2288】 生日礼物
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2288 [算法] 先将这个序列的正负数合并起来,变成一个正负交替的序列 如果新序列的正 ...
- EOJ 1501/UVa The Blocks Problem
Many areas of Computer Science use simple, abstract domains for both analytical and empirical studie ...
- 687C
dp 以前做过 忘了. 想破脑袋不知道怎么设状态 dp[i][j][k]表示选到第i个硬币,当前和为j,能否弄出k dp[i][j][k]|=dp[i-1][j][k]|dp[i-1][j][k-c[ ...
- springboot启动报错:Cannot determine embedded database driver class for database type NONE.
package cn.zb.test; import org.springframework.boot.SpringApplication; import org.springframework.bo ...
- C# 取两位小数
double s=0.55555;result=s.ToString("#0.00");//点后面几个0就保留几位 如果要四舍五入的话,用这个double dbdata = 0.5 ...