• cf666 C. Codeword 组合数学 离线分块思想


     
     
     
     
     
     
     
     
     
    time limit per test
    6 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The famous sculptor Cicasso is a Reberlandian spy!

    These is breaking news in Berlandian papers today. And now the sculptor is hiding. This time you give the shelter to the maestro. You have a protected bunker and you provide it to your friend. You set the security system in such way that only you can open the bunker. To open it one should solve the problem which is hard for others but is simple for you.

    Every day the bunker generates a codeword s. Every time someone wants to enter the bunker, integer n appears on the screen. As the answer one should enter another integer — the residue modulo 109 + 7 of the number of strings of length n that consist only of lowercase English letters and contain the string s as the subsequence.

    The subsequence of string a is a string b that can be derived from the string a by removing some symbols from it (maybe none or all of them). In particular any string is the subsequence of itself. For example, the string "cfo" is the subsequence of the string "codeforces".

    You haven't implemented the algorithm that calculates the correct answers yet and you should do that ASAP.

    Input

    The first line contains integer m (1 ≤ m ≤ 105) — the number of the events in the test case.

    The second line contains nonempty string s — the string generated by the bunker for the current day.

    The next m lines contain the description of the events. The description starts from integer t — the type of the event.

    If t = 1 consider a new day has come and now a new string s is used. In that case the same line contains a new value of the string s.

    If t = 2 integer n is given (1 ≤ n ≤ 105). This event means that it's needed to find the answer for the current string s and the value n.

    The sum of lengths of all generated strings doesn't exceed 105. All of the given strings consist only of lowercase English letters.

    Output

    For each query of the type 2 print the answer modulo 109 + 7 on the separate line.

    Example
    Input
    3
    a
    2 2
    1 bc
    2 5
    Output
    51
    162626
    Note

    In the first event words of the form "a?" and "?a" are counted, where ? is an arbitrary symbol. There are 26 words of each of these types, but the word "aa" satisfies both patterns, so the answer is 51.

     

    题意:

    输入一个数q<=10^5,和一个字符串str,所有字符串都只包含小写英文字母

    接下来有q个操作:

    1 str 把原来的字符串替换成新的字符串

    2 n

    求:长度为n的,str为其的子串(不用连续)的字符串的个数 % (1e9+7)

    保证所有输入的字符串的长度之和 <= 10^5

    solution:

    首先,这一道题在计数的时候要注意重复的情况

    先计算所有,再减去重复的情况?这样太难算了

    这道题相当于要填一个长度为n的字符串,使得包含str这个子串

    考虑要避免重复,需要具有以下性质:

    设长度为n的字符串为t

    若t[i]是从str[j]这里拿的,t[i+k]是从str[j+1]拿的,则区间[i+1,i+k-1]这一段不能出现str[j+1]

    则可以避免重复

    令f(i,j)表示t填写了i个字符,此时指向str的第j个数的方案数

    init:f(0,0) = 1

    f(i,j) += f(k,j-1) * 25^(i-k-1)

    明显复杂度太大了O(n^3)

    但是从这一个递推我们发现,方案数只与str的长度len有关,与str的内容没有关系

    这样的话,只要一个三元组(len,n,t)即可确定一个答案了

    (其实是2元组(len,n),t是用来离线的时候确定第t个询问的)

    则可以推出公式,对于一个三元组(len,n,t):

    ans = sigma(C(x-1,len-1) * 25^(x-len) * 26^(n-x)), len <= x <= n

    如果预处理:

    jie[i] = i!

    inv[i] = i!的关于mod的逆元 = qp(jie[i],mod-2)

    则一次询问可以在O(n)的时间内得到答案,总复杂度O(n^2)还是不够

    考虑离线,先len小到大,n小到大排序询问

    则相同的len的元组都放在了一起

    对于当前的len:

    令f[i]表示3元组(len,i,t)的答案,把公式写成递推的形式:

    f[i] = 0 ,i < len

    f[i]= 1, i = len

    f[i] = 26 * f[i-1] + C(i-1,len-1) * 25^(i-len)  (组合数C可以O(1))

    则可以在O(n)的时间内对当前的len求出n=[1,MAXN-1]的答案,

    则可以同时处理掉一大批询问了

    对于len发生了变化的询问,只需要再更新一次f数组即可

    因为有所有输入的字符串的长度之和 <= 10^5,所以最坏的情况下,输入的字符串的长度分别为

    1,2,3,...,ma,则 (1+ma)*ma/2 <= 10^5,则ma的规模是在O(sqrt(n))的,

    即是说,最多我们需要更新O(sqrt(n))次f数组,一次更新是O(n)的,所以总复杂度为

    O(n^1.5)

    这样的复杂度就可以接受了

    ps:

    刚开始没有预处理逆元,所以求组合数C要O(logn),总复杂度O(n^1.5 * logn),TLE了

    代码:

      1                                             
      2   //File Name: cf666C.cpp
      3   //Author: long
      4   //Mail: 736726758@qq.com
      5   //Created Time: 2016年05月20日 星期五 14时28分15秒
      6                                    
      7 
      8 #include <stdio.h>
      9 #include <string.h>
     10 #include <algorithm>
     11 #include <iostream>
     12 
     13 #define LL long long
     14 
     15 using namespace std;
     16 
     17 const int MAXN = 100000 + 3;
     18 const int MOD = (int)1e9 + 7;
     19 
     20 LL jie[MAXN], p25[MAXN],inv[MAXN];
     21 LL f[MAXN],ans[MAXN];
     22 
     23 struct Query{
     24     int len,n,t;
     25     Query(int _len = 0,int _n = 0,int _t = 0){
     26         len = _len,n = _n,t = _t;
     27     }
     28     bool operator < (const Query & a) const{
     29         if(len == a.len)
     30             return n < a.n;
     31         return len < a.len;
     32     }
     33 }q[MAXN];
     34 
     35 LL qp(LL x,LL y){
     36     LL res = 1;
     37     while(y){
     38         if(y & 1) res = res * x % MOD;
     39         x = x * x % MOD;
     40         y >>= 1;
     41     }
     42     return res;
     43 }
     44 
     45 void init(){
     46     jie[0] = 1;
     47     for(int i=1;i<MAXN;i++)
     48         jie[i] = jie[i-1] * i % MOD;
     49     p25[0] = 1;
     50     for(int i=1;i<MAXN;i++){
     51         p25[i] = p25[i-1] * 25 % MOD;
     52     }
     53     for(int i=0;i<MAXN;i++)
     54         inv[i] = qp(jie[i],MOD - 2);
     55 }
     56 
     57 LL get_c(LL x,LL y){
     58     if(x < 0 || x < y) return 0;
     59     if(y == 0 || y == x) return 1;
     60     return jie[x] * inv[y] % MOD * inv[x-y] % MOD;
     61 }
     62 
     63 char str[MAXN];
     64 
     65 void update(int len,int N){
     66     for(int i=0;i<len;i++)
     67         f[i] = 0;
     68     f[len] = 1;
     69     for(int i=len+1;i<=N;i++){
     70         f[i] = f[i-1] * 26 % MOD + get_c(i-1,len-1) * p25[i-len] % MOD;
     71         f[i] %= MOD;
     72     }
     73 }
     74 
     75 void solve(int tot){
     76     init();
     77     sort(q,q+tot);
     78     int pre = -1;
     79     for(int i=0;i<tot;i++){
     80         if(q[i].len == pre){
     81             ans[q[i].t] = f[q[i].n];
     82         }
     83         else{
     84             pre = q[i].len;
     85             int now = i;
     86             while(now < tot - 1 && q[now+1].len == q[now].len){
     87                 now++;
     88             }
     89             update(pre,q[now].n);
     90             ans[q[i].t] = f[q[i].n];
     91         }
     92     }
     93     for(int i=0;i<tot;i++)
     94         printf("%d
    ",(int)ans[i]);
     95 }
     96 
     97 int main(){
     98     int op,len,tot = 0;
     99     scanf("%d",&op);
    100     scanf("%s",str);
    101     len = strlen(str);
    102     for(int i=1,u,n;i<=op;i++){
    103         scanf("%d",&u);
    104         if(u == 1){
    105             scanf("%s",str);
    106             len = strlen(str);
    107         }
    108         else{
    109             scanf("%d",&n);
    110             q[tot] = Query(len,n,tot);
    111             tot++;
    112         }
    113     }
    114     solve(tot);
    115     return 0;
    116 }
  • 相关阅读:
    Visual Studio Code的常用快捷键
    requests模块
    爬虫基本原理
    Django-分页、中间件和请求的声明周期
    Django-admin管理工具
    Django-form表单
    Django-认证系统
    cookie、session和token
    Ajax
    Django进阶Model篇—数据库操作(ORM)
  • 原文地址:https://www.cnblogs.com/-maybe/p/5512744.html
Copyright © 2020-2023  润新知