UVALive - 3490 Generator 【数学】【高斯消元】

4 篇文章 0 订阅
2 篇文章 0 订阅
We can generate a random string by generating a sequence of random characters and concatenating
them together. Each character is chosen independently from the first n letters in the English alphabet
with equal probability. Only capital letters are used in this problem. The generation is stopped as soon
as a specific pattern occurs in the random string.
Your task is to predict the expected length of the generated string.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 ≤
T ≤ 10) which is the number of test cases. T test cases follow, each preceded by a single blank line.
Each test case consists of a single integer N (1 ≤ N ≤ 26) which is the number of letters used, and
a pattern, which is a non-empty string consisting of letters chosen from the first N upper case English
letters. The length of any pattern will not exceed 12.
Output
Results should be directed to standard output. Start each case with ‘Case #:’ on a single line, where
# is the case number starting from 1. Two consecutive cases should be separated by a single blank
line. No blank line should be produced after the last test case.
For each test case, print the expected length of the generated random string.
Sample Input
5
2 A
2 ABA
3 AAAAA
26 ACMICPC
26 ZJUZJU
Sample Output
Case 1:
2

Case 2:
10

Case 3:
363

Case 4:
8031810176

Case 5:
308933352

mp,n使
x[i]=i,|0<=i<m
c[i][j]=ij
x[i]=1+(1/n)n1j=0x[c[i][j]]
p,m

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<queue>
#include<sstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int inf=1e9+7;
const int N=26+3;
int c[N][N];

bool endWith(const string&str,const string&p,const int len){
    int i=str.size()-1;
    int j=len-1;
    while(j>=0){
        if(str[i]!=p[j]){
            return false;
        }
        --j,--i;
    }
    return true;
}

void init(const string&p,const int n){
    for(int i=0;i<p.size();++i){
        for(int j=0;j<n;++j){
            string str(p.begin(),p.begin()+i);
            str+='A'+j;
            for(int len=min(p.size(),str.size());len>=0;--len){
                if(endWith(str,p,len)){
                    c[i][j]=len;
                    break;
                }
            }

        }
    }
}

struct Num{
    ll fz,fm;
    Num(){
        fz=0,fm=1;
    }

    Num(ll a,ll b):fz(a),fm(b){}

    Num operator*(const Num&x){
        Num ans;
        ans.fz=fz*x.fz;
        ans.fm=fm*x.fm;
        ans.relax();
        return ans;
    }

    Num operator/(const Num&x){
        Num ans;
        ans.fz=fz*x.fm;
        ans.fm=fm*x.fz;
        ans.relax();
        return ans;
    }

    Num operator-(const Num&x){
        Num ans;
        ans.fz=fz*x.fm-fm*x.fz;
        ans.fm=fm*x.fm;
        ans.relax();
        return ans;
    }

    Num&operator-=(const Num&x){
        *this=*this-x;
        return *this;
    }

    void relax(){
        ll d=gcd(fz,fm);
        fz/=d;
        fm/=d;
    }

    ll gcd(ll a,ll b){
        return b?gcd(b,a%b):a;
    }
};

Num matrix[N][N];//保存增广矩阵
//int copyMatrix[N][N];//增广矩阵的副本
bool isFree[N];
Num ans[N];

int Gauss(Num a[][N],const int&m,const int&n,bool isFree[],Num ans[]){//m:变元个数 n:方程个数
    int res=0,r=0;//res为自由变元个数 r为增广矩阵的秩
    fill(isFree,isFree+n,false);
    for(int i=0;i<m;++i){//处理第i个变元
        for(int j=r;j<n;++j)//找到第i个变元系数不为0的方程 并放到第r行
            if(a[j][i].fz){
                for(int k=i;k<=m;++k)
                    swap(a[j][k],a[r][k]);
                break;
            }
        if(a[r][i].fz==0){//第i个变元没有系数不为0的 这变元是自由变元
            ++res;
            isFree[i]=true;
            continue;
        }
        for(int j=0;j<n;++j)//消去其他方程的i变元
            if((j!=r)&&(a[j][i].fz!=0)){
                Num t=a[j][i]/a[r][i];
                for(int k=i;k<=m;++k)
                    a[j][k]-=t*a[r][k];
            }
        ++r;//矩阵的秩+1
    }

    for(int i=0,j=0;i<n;++i){//求解
        if(isFree[i]){
            continue;
        }
        ans[i]=a[j][m]/a[j][i];
        ++j;
    }

    //矩阵的秩下面的方程 系数都为0 0*x1+0*x2+...+0*xm恒等于0
    for(int i=r;i<n;++i)//如果矩阵形如(0,0,0...0,a) a!=0 无解
        if(a[i][m].fz)//判断是否无解
            return -1;
    return res;//返回自由变元个数
}

void initMatrix(const int n,int m){//n:方程变量数 m字母数量
    for(int i=0;i<n;++i){
        fill(matrix[i],matrix[i]+n+2,Num());
    }
    for(int i=0;i<n;++i){
        matrix[i][i]=Num(m,1);
        matrix[i][n]=Num(m,1);
        for(int j=0;j<m;++j){
            if(c[i][j]!=n){
                matrix[i][c[i][j]]-=Num(1,1);
            }
        }
    }
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;++t){
        printf("Case %d:\n",t);
        int n;
        scanf("%d",&n);
        string p;
        cin>>p;
        init(p,n);//初始化c[N][N]
        initMatrix(p.size(),n);
        Gauss(matrix,p.size(),p.size(),isFree,ans);

        printf("%lld\n",ans[0].fz/ans[0].fm);

        if(t!=T){
            putchar('\n');
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值