Advertisement
yuhung94

高斯消去

Nov 5th, 2022
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #pragma GCC optimzize("Ofast,no-stack-protector")
  2. #include<bits/stdc++.h>
  3. #define int long long
  4. #define quick ios::sync_with_stdio(0);cin.tie(0);
  5. #define rep(x,a,b) for(int x=a;x<=b;x++)
  6. #define repd(x,a,b) for(int x=a;x>=b;x--)
  7. #define lowbit(x) (x&-x)
  8. #define sz(x) (int)(x.size())
  9. #define F first
  10. #define S second
  11. #define all(x) x.begin(),x.end()
  12. #define mp make_pair
  13. #define eb emplace_back
  14. using namespace std;
  15. typedef pair<int,int> pii;
  16. void debug(){
  17.     cout<<"\n";
  18. }
  19. template <class T,class ... U >
  20. void debug(T a, U ... b){
  21.     cout<<a<<" ",debug(b...);
  22. }
  23. const int N=1e3+7;
  24. const int INF=1e18;
  25. vector<int> v[N];
  26. int M;
  27. int qpow(int a,int b){
  28.     int ret=1;
  29.     while(b>0){
  30.         if(b&1) ret=ret*a%M;
  31.         a=a*a%M;
  32.         b>>=1;
  33.     }
  34.     return ret;
  35. }
  36. int inv(int x){
  37.     return qpow(x,M-2);
  38. }
  39. signed main(){
  40.     quick
  41.     int n;
  42.     cin>>n>>M;
  43.     vector<int> st(n);
  44.     rep(i,0,n-1) cin>>st[i];
  45.     rep(i,0,n-1){
  46.         rep(j,0,n-1){
  47.             int x;
  48.             cin>>x;
  49.             v[j].eb(x);
  50.         }
  51.     }
  52.     rep(i,0,n-1){
  53.         v[i].eb(st[i]);
  54.     }
  55.  
  56.     rep(i,0,n-1){
  57.         if(!v[i][i]){
  58.             rep(j,i+1,n-1){
  59.                 if(v[j][i]){
  60.                     swap(v[j],v[i]);
  61.                     break;
  62.                 }
  63.             }
  64.         }
  65.         rep(j,i+1,n-1){
  66.             int mul=(M-v[j][i]%M*inv(v[i][i])%M)%M;
  67.             rep(x,i,n){
  68.                 v[j][x]=(v[j][x]+mul*v[i][x])%M;
  69.             }
  70.         }
  71.     }
  72.     vector<int> ans(n);
  73.     repd(i,n-1,0){
  74.         int s=v[i][n];
  75.         rep(j,i+1,n-1){
  76.             s=s-v[i][j]*ans[j]%M;
  77.             s=(s+M)%M;
  78.         }
  79.         ans[i]=s*inv(v[i][i])%M;
  80.     }
  81.     rep(i,0,n-1){
  82.         cout<<ans[i]<<" \n"[i==n-1];
  83.     }
  84.  
  85.     return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement