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_ ...
随机推荐
- 设计模式原来如此-单例模式(Singleton Pattern)
简单介绍一下我对Singleton的理解,说的不好请大家多多指点. 单例模式的简单定义就是确保一个类只有一个实例,并提供一个全局访问点. 单例模式有哪些用处呢? 有一些对象其实我们只需要一个,比方说: ...
- 微信小程序 wx.navigateTo()传参及多个参数方法
var workModeAndPriceList = res.data.data.workModeAndPriceList; //var result = JSON.stringify(workMod ...
- 51 NOD 1407 and and and and !!
首先与等于零 相当于要求 每一位 在选的数里都有至少一个在该位为 0.直接求这个不太好求,我们考虑容斥: 设F(s) 为 不合法的位的集合至少是s的方案数 ,某一位不合法当且仅当选的数在这一位都是 ...
- js/jq仿window文件夹框选操作插件
0.先给大家看看效果: 1.创建一个index.html文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...
- 【原创】关于jquery实现格式化时间
//js格式化时间,参数jsonDate可以是后台数据 function jsonDateFormat(jsonDate) { try { var date = new Date(parseInt(j ...
- mySql 主从复制linux配置
总结: 主库(192.168.1.251): /etc/my.cnf.d/server.cnf [mysqld] log-bin=mysql-bin server-id=1 从库(192.168.1. ...
- [Hibernate开发之路](3)基础配置
一 JDBC链接 通常你希望SessionFactory来为你创建和缓存(pool)JDBC连接. 假设你採用这样的方式, 仅仅须要例如以下例所看到的那样,打开一个Session: Session s ...
- LINUX下目标文件的BSS段、数据段、代码段
http://blog.chinaunix.net/uid-27018250-id-3867588.html bss 未初始化的全局数据 data 已经初始化的全局数据 text 代码段,机器指令 r ...
- at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:)
错误提示 错误原因 參考链接 错误提示: at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:) 在Andro ...
- mysql性能优化-慢查询分析、优化索引和配置 MySQL索引介绍
MySQL索引介绍 聚集索引(Clustered Index)----叶子节点存放整行记录辅助索引(Secondary Index)----叶子节点存放row identifier-------Inn ...