关闭

poj 2947

149人阅读 评论(0) 收藏 举报
分类:

题目描述:

就是求模7的方程组.然后结果在3到9之间.n~300

题解:

高斯消元.用公倍数和除以逆元来解.

重点:

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctype.h>
#include <limits.h>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#define CLR(a) memset(a, 0, sizeof(a))
#define REP(i, a, b) for(int i = a;i < b;i++)
#define REP_D(i, a, b) for(int i = a;i <= b;i++)

typedef long long ll;

using namespace std;

const int maxn = 300+100;
const int MOD=7;

int m,n;
int a[maxn][maxn], x[maxn];
int equ, var;

inline int gcd(int a,int b)
{
    while(b != 0)
    {
        int t = b;
        b = a%b;
        a = t;
    }
    return a;
}
inline int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}
long long inv(long long a,long long m)
{
    if(a == 1)return 1;
    int t = pow(a, m-2) + 1e-9;
    return t%MOD;
}
int gauss()
{
    int k, col;
    for(k=0,col=0; k<equ&&col<var; k++,col++)
    {
        int max_r = k;
        for(int i = k+1; i < equ; i++)
            if(abs(a[i][col]) > abs(a[max_r][col]))
                max_r = i;
        if(a[max_r][col] == 0)
        {
            k--;
            continue;
        }
        if(max_r != k)
            for(int j = col; j < var+1; j++)
                swap(a[k][j],a[max_r][j]);
        for(int i=k+1; i<equ; i++)
        {
            if(a[i][col]!=0)
            {
                int d = lcm(abs(a[i][col]), abs(a[k][col]) );
                int d_k = d/a[k][col], d_i = d/a[i][col];
                if(a[i][col]*a[k][col]<0)
                    d_i=-d_i;
                for(int j=col; j<=var; j++)
                {
                    int lft = ((a[i][j]*d_i-a[k][j]*d_k)%MOD+MOD)%MOD;
                    a[i][j]=lft;
                }
            }
        }
    }
    for(int i = k; i < equ; i++)
        if(a[i][col] != 0)
            return -1;
    if(k < var) return var-k;
    for(int i=var-1; i>=0; i--)
    {
        int temp = a[i][var];
        for(int j=i+1; j<var; j++)
        {
            temp = ((temp-(a[i][j]*x[j])%MOD)%MOD+MOD)%MOD;
        }
        x[i] = (temp*inv(a[i][i],MOD))%MOD;
    }
    return 0;
}
void solve()
{
    equ=m;
    var=n;
    int t=gauss();
    if(t==-1)
    {
        printf("Inconsistent data.\n");
    }
    else if(t>0)
    {
        printf("Multiple solutions.\n");
    }
    else
    {
        for(int i=0; i<var; i++)
        {
            if(x[i]<=2)
                x[i] += 7;
            printf("%d%c", x[i], (i==var-1 ? '\n' : ' '));
        }
    }
}
int change(char s[])
{
    if(strcmp(s,"MON") == 0) return 1;
    else if(strcmp(s,"TUE")==0) return 2;
    else if(strcmp(s,"WED")==0) return 3;
    else if(strcmp(s,"THU")==0) return 4;
    else if(strcmp(s,"FRI")==0) return 5;
    else if(strcmp(s,"SAT")==0) return 6;
    else return 7;
}
int main()
{
    freopen("5Ein.txt", "r", stdin);
    //freopen("5Eout.txt", "w", stdout);
    while(scanf("%d%d",&n,&m) == 2)
    {
        if(n == 0 && m == 0)break;
        memset(a,0,sizeof(a));
        char str1[10],str2[10];
        int k;
        for(int i = 0; i < m; i++)
        {
            scanf("%d%s%s",&k,str1,str2);
            a[i][n] = ((change(str2) - change(str1) + 1)%MOD + MOD)%MOD;
            int t;
            while(k--)
            {
                scanf("%d",&t);
                t--;
                a[i][t] ++;
                a[i][t]%=MOD;
            }
        }
        solve();
    }
    return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:43549次
    • 积分:2405
    • 等级:
    • 排名:第15507名
    • 原创:206篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    最新评论