UVA 10951 - Polynomial GCD

题目链接

题意:给定两个多项式,求多项式的gcd,要求首项次数为1,多项式中的运算都%n,而且n为素数.

思路:和gcd基本一样,仅仅只是传入的是两个多项式,因为有%n这个条件。所以计算过程能够用乘法逆去计算除法模,然后最后输出的时候每项除掉首项的次数就是答案了.

代码:

#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std; int n;
vector<int> f, g; int exgcd(int a, int b, int &x, int &y) {
if (!b) {x = 1; y = 0; return a;}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
} int inv(int a, int n) {
int x, y;
exgcd(a, n, x , y);
return (x + n) % n;
} vector<int> pmod(vector<int> f, vector<int> g) {
int fz = f.size(), gz = g.size();
for (int i = 0; i < fz; i++) {
int k = fz - i - gz;
if (k < 0) break;
int a = f[i] * inv(g[0], n) % n;
for (int j = 0; j < gz; j++) {
int now = i + j;
f[now] = ((f[now] - a * g[j] % n) % n + n) % n;
}
}
vector<int> ans;
int p = -1;
for (int i = 0; i < fz; i++) if (f[i] != 0) {p = i; break;}
if (p >= 0) for (int i = p; i < fz; i++) ans.push_back(f[i]);
return ans;
} vector<int> gcd(vector<int> f, vector<int> g) {
if (g.size() == 0) return f;
return gcd(g, pmod(f, g));
} int main() {
int cas = 0;
while (~scanf("%d", &n) && n) {
f.clear(); g.clear();
int a, k;
scanf("%d", &a);
for (int i = 0; i <= a; i++) {
scanf("%d", &k);
f.push_back(k);
}
scanf("%d", &a);
for (int i = 0; i <= a; i++) {
scanf("%d", &k);
g.push_back(k);
}
vector<int> ans = gcd(f, g);
int tmp = inv(ans[0], n), ansz = ans.size();;
printf("Case %d: %d", ++cas, ansz - 1);
for (int i = 0; i < ansz; i++) {
printf(" %d", ans[i] * tmp % n);
}
printf("\n");
}
return 0;
}

UVA 10951 - Polynomial GCD(数论)的更多相关文章

  1. uva 10951 - Polynomial GCD(欧几里得)

    题目链接:uva 10951 - Polynomial GCD 题目大意:给出n和两个多项式,求两个多项式在全部操作均模n的情况下最大公约数是多少. 解题思路:欧几里得算法,就是为多项式这个数据类型重 ...

  2. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  3. UVa 1642 - Magical GCD(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. 【数论】UVa 10586 - Polynomial Remains

    Problem F: Polynomial Remains Given the polynomial a(x) = an xn + ... + a1 x + a0, compute the remai ...

  5. UVa 1642 Magical GCD (暴力+数论)

    题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点 ...

  6. UVA 10627 - Infinite Race(数论)

    UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ...

  7. uva 10555 - Dead Fraction)(数论)

    option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=1496" st ...

  8. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  9. 【UVA 11426】gcd之和 (改编)

    题面 \(\sum_{i=1}^{n}\sum_{j=1}^m\gcd(i,j)\mod998244353\) \(n,m<=10^7\) Sol 简单的一道莫比乌斯反演题 \(原式=\sum_ ...

随机推荐

  1. js、jq平时积累

    1.window.onbeforeunload   =   function(){$(window).scrollTop(0);}  //在即将离开当前页面(刷新或关闭)时执行 JavaScript ...

  2. delphi中关于时间差的实例

    http://www.cnblogs.com/rogee/archive/2010/09/20/1832035.html 很多时候要用到相差多少天,多少周,多少秒,查了一下资料,整理如下: 首先 us ...

  3. HTML-在canvas画图中,图片的线上链接已配置允许跨域后,仍然出错提示跨域,怎么解决?

    这个问题我已经遇到了2次,第一次解决了后,第二次又遇到了,所以这次做个笔记,怕以后再次遇到 举例: 1.要实现的问题:我需要在canvas画布上画上我的微信头像 2.后台配置已经完成了允许我头像地址的 ...

  4. 牛客网 Wannafly挑战赛9 C.列一列-sscanf()函数

      C.列一列   时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288K64bit IO Format: %lld 链接:https://www.now ...

  5. Oracle8i Internal Services

    http://d.hatena.ne.jp/yohei-a/20091017/1255791152 第1回 Oracle8i Internal Services 1人読書会 Oracle 読んだところ ...

  6. 3.环境搭建-Hadoop(CDH)集群搭建

    目录 目录 实验环境 安装 Hadoop 配置文件 在另外两台虚拟机上搭建hadoop 启动hdfs集群 启动yarn集群 本文主要是在上节CentOS集群基础上搭建Hadoop集群. 实验环境 Ha ...

  7. 转: How to Install MongoDB 3.2 on CentOS/RHEL & Fedora (简单易懂)

    from:  http://tecadmin.net/install-mongodb-on-centos-rhel-and-fedora/ MongoDB (named from “huMONGOus ...

  8. phonegap工程搭建基础(一)

      官网:http://cordova.apache.org   一.环境配置 1. 安装Cordova   on OS X and Linux: $ sudo npm install -g cord ...

  9. [ssh新闻公布系统三]存储新闻

    一.存储新闻dao方法 在NewsDao.java中新增存储新闻的saveOrupdate方法 public void saveOrupdate(News news){ getSession().sa ...

  10. DDR 布线规则

    https://blog.csdn.net/cpf099/article/details/52038862 https://blog.csdn.net/cpf099/article/details/5 ...