//4_3_2: Modular multiplication of polynomials 多项式相乘再取模 POJ1060 ZOJ1026
//f(x) * g(x) % h(x)
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int compare(int mul[],int mul_num,int h[],int h_num)
{
if(mul_num > h_num) return 1;
if(h_num > mul_num) return -1;
for(int i = mul_num - 1;i >= 0;i --)
{
if(mul[i] > h[i]) return 1;
else if(h[i] > mul[i]) return -1;
}
return 0;
}
int main()
{
int i,j,n,f_num,g_num,h_num,mul_num;
int f[1005],g[1005],h[1005],mul[2010];
scanf("%d",&n);
while(n--)
{
memset(mul,0,sizeof(mul));
scanf("%d",&f_num);
for(i = f_num - 1;i >= 0;i --) scanf("%d",&f[i]);
scanf("%d",&g_num);
for(i = g_num - 1;i >= 0;i --) scanf("%d",&g[i]);
scanf("%d",&h_num);
for(i = h_num - 1;i >= 0;i --) scanf("%d",&h[i]);
for(i = f_num - 1;i >= 0;i --)
for(j = g_num - 1;j >= 0;j --)
mul[i + j] ^= (f[i] & g[j]);
mul_num = f_num + g_num - 1;
while(compare(mul,mul_num,h,h_num) >= 0)
{
int dis = mul_num - h_num;
for(i = 0;i < h_num;i ++) mul[i + dis] ^= h[i];
while(mul_num && !mul[mul_num - 1]) mul_num --;
}//mul_num不能为0,最少为1,就算余数是0,正好整除,也要输出余数0
if(mul_num == 0) mul_num = 1;
cout << mul_num << ' ';
for(i = mul_num - 1;i > 0;i --)
cout << mul[i] << ' ';
cout << mul[0] << endl;
}
return 0;
}
/*测试结果:通过POJ1060 ZOJ1026检测
2
7 1 0 1 0 1 1 1
8 1 0 0 0 0 0 1 1
9 1 0 0 0 1 1 0 1 1
8 1 1 0 0 0 0 0 1
10 1 1 0 1 0 0 1 0 0 1
12 1 1 0 1 0 0 1 1 0 0 1 0
15 1 0 1 0 1 1 0 1 1 1 1 1 0 0 1
14 1 1 0 1 1 0 0 1 1 1 0 1 0 0
请按任意键继续. . .
*/
POJ1060 ZOJ1026 Modular multiplication of polynomials
最新推荐文章于 2016-10-15 13:16:12 发布