UVA 10951 - Polynomial GCD(数论)
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(数论)的更多相关文章
- uva 10951 - Polynomial GCD(欧几里得)
题目链接:uva 10951 - Polynomial GCD 题目大意:给出n和两个多项式,求两个多项式在全部操作均模n的情况下最大公约数是多少. 解题思路:欧几里得算法,就是为多项式这个数据类型重 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- UVa 1642 - Magical GCD(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【数论】UVa 10586 - Polynomial Remains
Problem F: Polynomial Remains Given the polynomial a(x) = an xn + ... + a1 x + a0, compute the remai ...
- UVa 1642 Magical GCD (暴力+数论)
题意:给出一个长度在 100 000 以内的正整数序列,大小不超过 10^ 12.求一个连续子序列,使得在所有的连续子序列中, 它们的GCD值乘以它们的长度最大. 析:暴力枚举右端点,然后在枚举左端点 ...
- UVA 10627 - Infinite Race(数论)
UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ...
- uva 10555 - Dead Fraction)(数论)
option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=1496" st ...
- [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535 Descriptio ...
- 【UVA 11426】gcd之和 (改编)
题面 \(\sum_{i=1}^{n}\sum_{j=1}^m\gcd(i,j)\mod998244353\) \(n,m<=10^7\) Sol 简单的一道莫比乌斯反演题 \(原式=\sum_ ...
随机推荐
- kd树的构造与搜索
学习了两篇博客,存下来以免丢失. http://blog.csdn.net/losteng/article/details/50893739 https://leileiluoluo.com/post ...
- 转---派遣例程与IRP结构
派遣例程与IRP结构 文章出处:http://www.cnblogs.com/zmlctt/p/3978124.html#commentform 提到派遣例程,必须理解IRP(I/O Requ ...
- es6 const let
一.const 1.const 声明的是常量,一旦声明,值将是不可变的: 2.const也具有块级作用域: 3.不能变量提升(必须先声明后使用): 4.const 不可重复声明 5.const 指令指 ...
- Codeforces Gym101572 B.Best Relay Team (2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017))
2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017) 今日份的训练,题目难度4颗星,心态被打崩了,会的算法太少了,知 ...
- java多线程03-----------------volatile内存语义
java多线程02-----------------volatile内存语义 volatile关键字是java虚拟机提供的最轻量级额的同步机制.由于volatile关键字与java内存模型相关,因此, ...
- Maven创建Java Application工程(既jar包)
Maven在创建工程时使用的是archetype(原型)插件,而如果要创建具体的工程,比如Application这些,那么可以使用maven-archetype-quickstart(相当于一个子类型 ...
- MongoDb 判断字段长度比较好的方法
查询某字段长度超过一定长度时的方法, MongoDB中可能不好处理,一般这样: db.test.find({ $where:"this.F_DAQDATA.legnth>600&quo ...
- scrapy 启动失败,scrapy startproject test 出错 'module' object has no attribute 'OP_NO_TLSv1_1
你先看看 pip install scrapy需要的 pyopenssl twisted 等和你安装的版本一样么 我的就是因为TWist 版本高于 需要的 用pip install twist ...
- iOS开发 Coretext基本用法
转至 http://blog.csdn.net/fengsh998/article/details/8691823 API接口文档. https://developer.apple.com/libra ...
- AngularJS的ng-repeat显示表格
代码下载:https://files.cnblogs.com/files/xiandedanteng/angualrJSngRepeatTable.rar 代码: <!DOCTYPE HTML ...