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. os.system() 和 os.popen()

    1.os.popen(command[, mode[, bufsize]])  os.system(command) 2.os.popen() 功能强于os.system() , os.popen() ...

  2. 使用Zint和Zxing实现二维码的编码与解码(Using open-source tools to generate and decode Q-R code)

    1.Zint生成二维码 http://sourceforge.net/projects/zint/ [1]从上述站点下载Zint工具 [2]安装Zint工具 [3]使用Zint工具生成二维码,注意选择 ...

  3. AC日记——太空飞行计划 洛谷 P2762

    题目背景 题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,Em},和进行这些实验需要使用的 ...

  4. CDOJ 3 BiliBili, ACFun… And More! 模拟

    原题链接:http://acm.uestc.edu.cn/#/problem/show/3 题意: 有个人在看B站视频时有个习惯,就是每当卡住的时候,他总再次从头开始看.另外,他在看视频时会先等待T的 ...

  5. BZOJ 1355 [Baltic2009] Bazinga

    题面 [题目描述] 对于给定的n个字符串S1,S2--,Sn,标号为1到n,请你找出一个最大的i使得标号小于i的字符串中存在一个不是i的子串. [输入描述]: 第一行包括一个整数t(1<=t&l ...

  6. @RequestParam,@PathVariable,@ResponseBody,@RequestBody,@ModelAttribute学习

    1.@RequestParam使用于参数上,用于将请求参数映射到指定参数变量上 例如: @RequestMapping(value="/hello",method=RequestM ...

  7. Linux下查看某个命令的参数

    1.一般每个命令都带有help参数,使用方法如下: shutdown --help 提示:shutdown为关机命令,在真实环境使用时需要root权限,比如前面加sudo. 2.使用man命令查看,使 ...

  8. 唤醒你的大脑 --- javascript冒泡排序

    var a; a = [1, 2, 3, 11, 55, 5, 0, 44]; (function bubbleSort() { for (var i = 0; i <= a.length - ...

  9. [反汇编练习] 160个CrackMe之029

    [反汇编练习] 160个CrackMe之029. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  10. Android HandlerThread 消息循环机制之源代码解析

    关于 HandlerThread 这个类.可能有些人眼睛一瞟,手指放在键盘上,然后就是一阵狂敲.立即就能敲出一段段华丽的代码: HandlerThread handlerThread = new Ha ...