poj 2065 高斯消元

来源:互联网 发布:windows smb远程漏洞 编辑:程序博客网 时间:2024/06/03 20:40

萌萌哒的高斯消元模板题
恩,这道题题目很鬼畜, ̄へ ̄,请各位拿好速效救心丸>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
原创粉丝点击