poj 2065 高斯消元

萌萌哒的高斯消元模板题
恩,这道题题目很鬼畜, ̄へ ̄,请各位拿好速效救心丸>o<
首先给出cas**case数目
每个case一行
首先输入p**此题再mod p下进行
然后输入一个字符串,字符串n个字母就有n个未知数
然后就有了n个方程
(a1*1^0+a2*1^1+……+an*1^n-1)%p=str[1]%p
.
.
.
.
.
.
(a1*n^0+a2*n^1+……+an*n^n-1)%p=str[n]%p
(⊙v⊙)嗯
然后你就把gauss模板一摆,就万事大吉啦~(≧▽≦)/~

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=70+10;
int cas,p,cnt,a[maxn],len,coefficient[maxn][maxn],ans[maxn];//每个方程的系数 
char str[maxn];
void gauss(){
    for(int i=1,j=1;i<=cnt&&j<=cnt;i++,j++){//j枚举第几个主元,i枚举第几个方程 
        int k;
        for(k=i;k<=cnt;k++)
            if(coefficient[k][j])
                break; //寻找用第k方程来消去其他方程的第j主元
        for(int s=1;s<=cnt+1;s++)
            swap(coefficient[i][s],coefficient[k][s]);
        for(int k=1;k<=cnt;k++){
            if(k!=i&&coefficient[k][j]){
                int b1=coefficient[k][j],b2=coefficient[i][j];
                for(int s=1;s<=cnt+1;s++)
                    coefficient[k][s]=((coefficient[k][s]*b2-coefficient[i][s]*b1)%p+p)%p;
            }
        }
    } 
    for(int i=cnt;i>0;i--){//第i个方程 倒着枚举可以保证下面枚举ans是都是已经求出的 
        int temp=coefficient[i][cnt+1];
        for(int j=i+1;j<=cnt;j++)
            temp=((temp-coefficient[i][j]*ans[j])%p+p)%p;
        while(temp%coefficient[i][i]!=0)
            temp+=p;
        ans[i]=(temp/coefficient[i][i])%p;
    }
}
int main(){
    scanf("%d",&cas);
    while(cas--){
        cnt=0,scanf("%d",&p),cin>>str,len=strlen(str);
        for(int i=0;i<len;i++){
            if(str[i]=='*')
                a[++cnt]=0;
            else
                a[++cnt]=(str[i]-'a'+1)%p;
            coefficient[cnt][len+1]=a[cnt];
        }
        for(int i=1;i<=cnt;i++)
            for(int j=1;j<=cnt;j++)
                if(j==1)
                    coefficient[i][1]=1;
                else
                    coefficient[i][j]=coefficient[i][j-1]*i%p;
        gauss();
        for(int i=1;i<cnt;i++)
            cout<<ans[i]<<" ";
        cout<<ans[cnt]<<endl;
    }
    return 0;
}

by >o< neighthorn

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值