poj2947 Widget Factory

题目描述

题解:

高消模$7$。

代码:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 305;
template<typename T>
inline void read(T&x)
{
    T f = 1,c = 0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}
    x = f*c;
}
int n,m,k,a[N][N];
char s[10];
int get()
{
    scanf("%s",s);
    switch (s[2])
    {
        case 'E':return 2;
        case 'D':return 3;
        case 'U':return 4;
        case 'I':return 5;
        case 'T':return 6;
    }
    switch (s[0])
    {
        case 'M':return 1;
        default :return 7;
    }
}
int inv[10]={0,1,4,5,2,3,6};
int gs()
{
    int l1,l2;
    for(l1=l2=1;l1<=m&&l2<=n;l1++,l2++)
    {
        int tmp = l1;
        while(tmp<=m&&!a[tmp][l2])tmp++;
        if(tmp>m){l1--;continue;}
        if(tmp!=l1)
            for(int j=l2;j<=n+1;j++)swap(a[l1][j],a[tmp][j]);
        int now = inv[a[l1][l2]];
        for(int i=l2;i<=n+1;i++)
            a[l1][i]=a[l1][i]*now%7;
        for(int i=l1+1;i<=m;i++)
        {
            now = a[i][l2];
            for(int j=l2;j<=n+1;j++)
                a[i][j]=(a[i][j]-now*a[l1][j]%7+7)%7;
        }
    }
    for(int i=l1;i<=m;i++)
        if(a[i][n+1])return -1;
    for(int i=n;i>=1;i--)
        for(int j=i-1;j>=1;j--)
            a[j][n+1]=(a[j][n+1]-a[i][n+1]*a[j][i]%7+7)%7;
    return n-l1+1;
}
int main()
{
//    freopen("tt.in","r",stdin);
    while(1)
    {
        read(n),read(m);
        if(!n&&!m)break;
        memset(a,0,sizeof(a));
        for(int x,i=1;i<=m;i++)
        {
            read(k);
            int fr = get();
            int to = get();
            a[i][n+1]=-fr+to+1;
            if(a[i][n+1]<0)a[i][n+1]+=7;
            if(a[i][n+1]>6)a[i][n+1]-=7;
            while(k--)
            {
                read(x);
                a[i][x]++;
                if(a[i][x]==7)a[i][x]=0;
            }
        }
        int k = gs();
        if(k==-1)puts("Inconsistent data.");
        else if(k>0)puts("Multiple solutions.");
        else
        {
            for(int i=1;i<=n;i++)
                printf("%d ",a[i][n+1]+(a[i][n+1]<3)*7);
            puts("");
        }
    }
    return 0;
}

 

posted @ 2019-03-04 23:08  LiGuanlin  阅读(128)  评论(0编辑  收藏  举报