Vasya and Shifts CodeForces - 832E (高斯消元)

大意: 给定$4n$个$m$位的五进制数, $q$个询问, 每个询问给出一个$m$位的五进制数$b$, 求有多少种选数方案可以使五进制异或和为$b$.

 

高斯消元入门题

每次询问相当于就是给定了$m$个式子组成的模$5$的方程组, 求解的个数

如果消元后询问某一位非零, 但是对应系数矩阵全零, 那么无解

否则解的个数是$5^{n-r}$

$q$组询问的话, 就增广$q$列, 同时解$q$个方程组即可.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 510;
int n, m, q, A[N][2*N], in[N];
char s[N];

int main() {
	REP(i,0,4) in[i]=i*i*i%5;
	scanf("%d%d", &n, &m);
	REP(i,1,n) {
		scanf("%s",s+1);
		REP(j,1,m) A[j][i]=s[j]-'a';
	}
	scanf("%d", &q);
	REP(i,1,q) {
		scanf("%s",s+1);
		REP(j,1,m) A[j][i+n]=s[j]-'a';
	}
	int r = 0;
	REP(i,1,n) {
		int p = r;
		while (!A[p][i]&&p<=m) ++p;
		if (p>m) continue;
		if (p!=r) REP(j,1,n+q) swap(A[p][j],A[r][j]);
		REP(j,1,m) if (j!=r&&A[j][i]) {
			int t = A[j][i]*in[A[r][i]]%5;
			REP(k,i,n+q) A[j][k]=(A[j][k]-t*A[r][k]+25)%5;
		}
		++r;
	}
	REP(i,1,q) {
		int ans = qpow(5,n-r);
		REP(j,1,m) if (A[j][i+n]) {
			int ok = 0;
			REP(k,1,n) if (A[j][k]) ok = 1;
			if (!ok) ans = 0;
		}
		printf("%d\n",ans);
	}
}

 

 

 

上一篇:鸡兔同笼


下一篇:快速幂详解