poj 2947 Widget Factory

http://poj.org/problem?id=2947

基础高斯消元   注意时刻模7

#include<iostream>

#include<cstdio>

#include<algorithm>

#include<cstring>

#include<map>

#include<cmath>

#define LL long long



using namespace std;



const int N=350;



int a[N][N];

int ans[N];

int n,m;

char s[9][5]={"","MON","TUE","WED","THU","FRI","SAT","SUN"};

int Date(char stemp[])

{

    for(int i=1;i<=7;++i)

    {

        if(strcmp(stemp,s[i])==0)

        return i;

    }

    return 0;

}

int findans(int x,int y)

{

    x=(x%7+7)%7;

    y=(y%7+7)%7;

    for(int i=3;i<=9;++i)

    {

        if(x*i%7==y)

        return i;

    }

}

void Gauss()

{

    int row=1;

    int col=1;

    while(row<=m&&col<=n)

    {

        int k=0;

        for(int i=row;i<=m;++i)

        if(a[i][col]!=0)

        {k=i;break;}

        if(k==0)//如果一列全为0 则列加一

        {++col;continue;}

        if(k!=row)//如果此行最前是0 则和不是0 的那一行交换

        for(int j=col;j<=n+1;++j)

        swap(a[row][j],a[k][j]);

        int x=a[row][col];

        for(int i=row+1;i<=m;i++)//把余下行的最前元素变成0 

        {

            int y=a[i][col];

            for(int j=col;j<=n+1;++j)

            {

                a[i][j]=(a[i][j]*x-y*a[row][j])%7;

            }

        }

        ++row;++col;

    }//这样最好形成的是右上三角

    for(int i=row;i<=m;++i)

    {

        if(a[i][n+1]!=0)//如果这样无解

        {

            printf("Inconsistent data.\n");

            return ;

        }

    }

    if(row!=col||row-1<n||col<=n)//row!=col 说明有一列全为0的状况 row-1<n说明A的秩小于n col<=n 说明列太长

    {

        printf("Multiple solutions.\n");

        return ;

    }

    for(int i=row-1;i>=1;--i)

    {

        int sum=0;

        for(int j=i+1;j<=n;++j)



        sum+=a[i][j]*ans[j];

        ans[i]=findans(a[i][i],a[i][n+1]-sum);

    }

    for(int i=1;i<=n;++i)

    {

        printf("%d",ans[i]);

        if(i==n)

        printf("\n");

        else

        printf(" ");

    }

}

int main()

{



    while(scanf("%d %d",&n,&m)!=EOF)

    {

        if(n==0&&m==0)

        break;

        memset(a,0,sizeof(a));

        int x;

        char d1[5],d2[5];

        for(int i=1;i<=m;++i)

        {

            scanf("%d %s %s",&x,d1,d2);

            //printf("%s %s\n",d1,d2);

            a[i][n+1]=Date(d2)-Date(d1)+1;

            while(x--)

            {

                int k;

                scanf("%d",&k);

                ++a[i][k];

            }

            for(int j=1;j<=n+1;++j)

            {

                a[i][j]%=7;

            }

        }

        Gauss();

    }

    return 0;

}

  

你可能感兴趣的:(factory)