![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<bits/stdc++.h> 2 #define N 100010 3 using namespace std; 4 typedef long long ll; 5 ll a[N]; 6 int p; 7 ll pow(ll y,int z,int p){ 8 y%=p;ll ans=1; 9 for(int i=z;i;i>>=1,y=y*y%p)if(i&1)ans=ans*y%p; 10 return ans; 11 } 12 ll C(ll n,ll m){ 13 if(m>n)return 0; 14 return ((a[n]*pow(a[m],p-2,p))%p*pow(a[n-m],p-2,p)%p); 15 } 16 ll Lucas(ll n,ll m){ 17 if(!m)return 1; 18 return C(n%p,m%p)*Lucas(n/p,m/p)%p; 19 } 20 inline int read(){ 21 int f=1,x=0;char ch; 22 do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9'); 23 do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9'); 24 return f*x; 25 } 26 int main(){ 27 int T=read(); 28 while(T--){ 29 int n=read(),m=read();p=read(); 30 a[0]=1; 31 for(int i=1;i<=p;i++)a[i]=(a[i-1]*i)%p; 32 cout<<Lucas(n+m,n)<<endl; 33 } 34 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include<cstdio> #include<vector> using namespace std; struct Matrix{ vector<vector<int> > matrix; int r,c; Matrix(){r=-1,c=-1;} inline void input(int row,int col) { int t; matrix.resize(row); r=row,c=col; for(int i=0;i<r;i++) for(int j=0;j<c;j++) { scanf("%d",&t); matrix[i].push_back(t); } } inline void print(int r,int c) { for(int i=0;i<r;i++) { for(vector<int>::iterator it=matrix[i].begin();it!=matrix[i].end();++it) printf("%d ",*it); printf("\n"); } } Matrix operator * (const Matrix& a)const { Matrix ret; if(this->c!=a.r&&this->r!=a.c)throw -1; ret.r=this->r,ret.c=a.c; ret.matrix.resize(ret.r); for(int i=0;i<ret.r;i++) for(int j=0;j<ret.c;j++) { ret.matrix[i].push_back(0); for(int k=0;k<this->c;k++) ret.matrix[i][j]+=a.matrix[k][j]*this->matrix[i][k]; } return ret; } }a,b,res; int r,c; int main() { scanf("%d%d",&r,&c); a.input(r,c); scanf("%d%d",&r,&c); b.input(r,c); try {res=a*b;} catch(...){printf("Unsupported Matrix.\n");return 0;} res.print(res.r,res.c); return 0; }
#include<cstdio> #include<vector> using namespace std; struct Matrix { vector<vector<long long> > matrix; int r,c; Matrix() { r=-1,c=-1; } inline void input(int row,int col) { int t; matrix.resize(row); r=row,c=col; for(int i=0; i<r; i++) for(int j=0; j<c; j++) { scanf("%d",&t); matrix[i].push_back(t); } } inline void print() { if(this->r<0||this->c<0){ printf("Invalid Matrix, Maybe Syntax Error Had Happened?\n"); return; } for(int i=0; i<this->r; i++) { for(vector<long long>::iterator it=matrix[i].begin(); it!=matrix[i].end(); ++it) printf("%I64d ",*it); printf("\n"); } } Matrix operator + (const Matrix& a)const { Matrix ret; if(this->r!=a.r||this->c!=a.c)throw -1; ret.r=this->r,ret.c=this->c; ret.matrix.resize(ret.r); for(int i=0;i<ret.r;i++) for(int j=0;j<ret.c;j++) ret.matrix[i].push_back(a.matrix[i][j]+this->matrix[i][j]); return ret; } Matrix operator - (const Matrix& a)const { Matrix ret; if(this->r!=a.r||this->c!=a.c)throw -1; ret.r=this->r,ret.c=this->c; ret.matrix.resize(ret.r); for(int i=0;i<ret.r;i++) for(int j=0;j<ret.c;j++) ret.matrix[i].push_back(this->matrix[i][j]-a.matrix[i][j]); return ret; } Matrix operator * (const Matrix& a)const { Matrix ret; if(this->c!=a.r&&this->r!=a.c)return ret; ret.r=this->r,ret.c=a.c; ret.matrix.resize(ret.r); for(int i=0; i<ret.r; i++) for(int j=0; j<ret.c; j++) { ret.matrix[i].push_back(0); for(int k=0; k<this->c; k++) ret.matrix[i][j]+=a.matrix[k][j]*this->matrix[i][k]; } return ret; } };