poj 1060 Modular multiplication of polynomials (多项式问题)

本文介绍了一种通过使用freopen函数来优化程序输入输出的方法,这种方法能够简化代码并提高效率。文中提供了一个具体的示例代码,展示了如何利用freopen进行文件的读取和输出,同时讨论了该方法在竞赛编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为freopen("in.txt","r",stdin);贡献了一个超时,以后就的最终代码就是把这行注释掉就行了。相比以前的

 freopen("in.txt","r",stdin);

 freopen("out.txt","w",stdout);

和最后的

 fclose(stdin);

 fclose(stdout);

四行冗余的代码来说,freopen("in.txt","r",stdin);一行足以(不但满足需求还顺眼),而且上交的时候只要注释一行即可。



#include<iostream>

#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;

int a[1005];
int b[1005];
int c[1005];
int cun[2005];
int cun1[2005];

int main()
{
    //freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    for(int i=0; i<t; i++)
    {
        int m,n,v;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(cun,0,sizeof(cun));
        memset(cun1,0,sizeof(cun1));
        scanf("%d",&m);
        for(int j=m-1; j>=0; j--)
            scanf("%d",&a[j]);
        scanf("%d",&n);
        for(int j=n-1; j>=0; j--)
            scanf("%d",&b[j]);
        scanf("%d",&v);
        for(int j=v-1; j>=0; j--)
            scanf("%d",&c[j]);
        ;
        for(int j=0; j<m; j++)
        {
            for(int k=0; k<n; k++)
            {
                cun[j+k]+=a[j]*b[k];
            }
        }
        for(int j=0; j<m+n-2; j++)
            cun[j]=cun[j]%2;
        int cnt=m+n-2;
        while(cnt-v+1>=0)
        {
            memset(cun1,0,sizeof(cun1));
            for(int j=0; j<v; j++)
                if(c[j])cun1[cnt-v+1+j]=1;
            int flag=1;
            for(int j=cnt; j>=0; j--)
            {
                cun[j]=abs(cun[j]-cun1[j]);
                if(flag&&cun[j]==1)
                {
                    cnt=j;
                    flag=0;
                }
            }
        }
        ;
        printf("%d",cnt+1);
        for(int j=cnt;j>=0;j--)
            printf(" %d",cun[j]);
        putchar('\n');
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值