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. [转]常用iOS图片处理方法

    自:http://blog.sina.com.cn/s/blog_8988732e0100xcx1.html  ========== (one) UIImage 图像 等比例缩放=========== ...

  2. ARCGIS SDK For DotNet 路径

    ARCGIS SDK For DotNet 路径 驱动器 C 中的卷是 WIN7 卷的序列号是 06AC-BD3E C:\Program Files (x86)\ArcGIS\DeveloperKit ...

  3. p标签注意事项

    <p> 哈哈,我是一个段落哦! <div id="box">我是一个萌萌的div</div> </p> 这样写的后果: 注意: p标 ...

  4. Ant -----ant标签和自定义任务

    随便记一下 Ant的用法吧.ant ,maven, gradle ,三个打包工具到齐了,Ant 常见标签解析,ant 自定义task . <?xml version="1.0" ...

  5. Spring IOC知识java反射

    [1] Java反射知识-->Spring IoC :http://www.iteye.com/topic/1123081 [2] Java动态代理-->Spring AOP :http: ...

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

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

  7. mac 下 virtualbox 配置全网通

    mac下virtualbox实现主机和虚拟机.虚拟机和外网互访的方案 全局添加Host-Only网络 Adapter IPv4 Address:192.168.56.1 IPv4 Network Ma ...

  8. 学习笔记 Java类的封装、继承和多态 2014.7.10

    1.问题:toString()没搞懂? int a = 1; Integer aa = new Integer(a); //这是实现的过程 System.out.println("Hello ...

  9. C# Ftp方式下载文件(无用户认证方式,支持断点续传)

    类代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  10. hadoop安全之hftp

    hftp默认是打开的,同意以浏览器的方式訪问和下载文件,以此方式下,能够读取全部文件,留下了安全隐患. 測试例如以下 /user/hive/warehouse/cdntest.db/selfreado ...