Educational Codeforces Round 164 (Rated for Div. 2) ----- F. Unique Strings ----- 题解

F. Unique Strings:

题目描述:

思路解析:

这里给出官方题解链接,然后再给出部分我对于这道题的理解,因为我组合数学真的还没学,可能理解有点偏差。

Educational Codeforces Round 164 Editorial - Codeforces

 题目给出一个字符串长度为n,然后前c个字符为1,后n-c个字符为0,你可以使用不超过k次操作就0变为1,那么这个字符串最多拥有k+c个1,假设现在拥有字符串a和b,如果a能通过循环移位变为b,那么就认为a和b是等价的。问满足互不等价的字符串数量有多少个。

那么根据置换群的概念,所有循环置换函数可以组成一个置换群。  那么我们假设把字符串字符从0改变为1看作是染色的话。那么就可以得到一个染色方案的集合。 那么对于置换群G,和染色方案G,那么我们怎么求在经过作用后染色方案的不等价数量呢。 根据Burnside定理有以下公式:

cnt 为 for(int i -= 1; i <= n; i++) cnt[gcd(n,i)]++; 求得cnt数组,上面公式具体证明看相关书籍。公式本质为染色方案中非等价的方案数等于在G中的作用下保持不变的着色的平均数。

那么我们再考虑,我们对于染色方案有其他要求需要满足,置换可以看作是几个循环,那么我们就可以将染色方案在置换作用下不变,看作是他在分为g组的情况下,组内的元数相等(这样的循环就一定相等),那么我们就可以利用g个前缀字符唯一定义一个字符串,那么每个字符会复制 n/g次后变为原字符串,那么这g个前缀中最多拥有 a = (k+c) / (n/g) 个1,那么等价于至少拥有 b = g - a个0,并且需要满足前缀中还是要包含连续的c个1,但是正面并不好求,那么我们利用容斥原理来求得。

现在有求出所有方案数 (长度为g的字符串中,只有1个1......有n个1的所有情况),再减去哪些没有连续c个1的方案数。令dp[z][l] 表示长度为l的字符串中,拥有z个0,并且字符串最后一个字符为0,并且没有连续c个1的方案数。

                                                                                                                                                   

代码实现:

import java.util.*;


import java.io.*;

public class Main {
    static long[] fac = new long[3005];
    static long[] inv = new long[3005];
    public static void main(String[] args) throws IOException {

        int t = 1;
        while (t > 0) {
            solve();
            t--;
        }
        w.flush();
        w.close();
    }
   static int mod = (int) 1e9 + 7;
    static long[][] dp = new long[3005][3005]; static long[][] sum = new long[3005][3005];
    static int n, c, k;
    public static void solve() throws IOException {
        n = f.nextInt(); c = f.nextInt(); k = f.nextInt();
        init();
        getDp(c);
        int[] cnt = new int[n+1];
        for (int i = 1; i <= n; i++) {
            cnt[gcd(n, i)]++;
        }
        long ans = 0;
        for (int i = 1; i <= n; i++) {
            if (n % i != 0) continue;
            long res = getRes(i);
//            w.println(i + " " + res);
            ans += res * cnt[i];
            ans %= mod;
        }
        w.println(ans * qkm(n) % mod);
    }

    static int gcd(int a, int b){
        if (a == 0) return b;
        if (b == 0) return a;
        return gcd(b, a % b);
    }
    static long getRes(int g){
        if (c >= g){
            return c + k >= n ? 1 : 0;
        }
        int oneCnt = (k + c) / (n / g); // 最多包含一的个数
        int zeroCnt = g - oneCnt; // 最少需要有的0的个数
        long ans = 0;
        for (int i = 0; i < oneCnt + 1; i++) {
            ans += C(g, i);
            ans %= mod;
        }
        for (int p = 0; p < c; p++) {
            int minZeros = Math.max(0, zeroCnt - 1);
            int mid = g - c;
            int suf = g - p - 1;
            for (int i = minZeros; i < suf + 1; i++) {
                ans -= (getSum(i, mid, suf+1) + mod) % mod;
                ans = (ans + mod) % mod;
            }

        }
        return ans;
    }

    public static long getSum(int z, int l , int r){
        if (z < 0 || z > n || l >= r) return 0;
        return sum[z][r] - sum[z][l];
    }


    static void getDp(int mx){
        dp[0][0] = 1;
        for (int z = 0; z < n; z++) {
            for (int l = 0; l < n; l++) {
                if (dp[z][l] == 0) continue;
                dp[z+1][l+1] = (dp[z+1][l+1] + dp[z][l]) % mod;
                // 这里可以保证,经过后面的前缀和运算后,保证dp[z][l]中长度为l,且含有z个0,且最后一个0,并且其中没有连续1的个数大于等于c的块
                int j = Math.min(n, l + mx) + 1;
                dp[z+1][j] = (dp[z+1][j] - dp[z][l]) % mod;
                dp[z+1][j] = (dp[z+1][j] + mod) % mod;
            }
            for (int i = 0; i < n; i++) {
                dp[z+1][i+1] = (dp[z+1][i+1] + dp[z+1][i]) % mod;
            }
        }
        for (int z = 0; z < n+1; z++) {
            for (int l = 0; l < n+1; l++) {
                sum[z][l+1] = (sum[z][l] + dp[z][l]) % mod;
            }
        }
    }


    static void init(){
        fac[0] = 1; inv[0] = 1;
        for (int i = 1; i < 3005; i++) {
            fac[i] = fac[i-1] * i % mod;
            inv[i] = qkm(fac[i]);
        }
    }

    static long C(int n, int i){
        if (i < 0 || i > n) return 0;
        return fac[n] * inv[i] % mod * inv[n - i] % mod;
    }

    static long qkm(long a){
        long b = mod - 2;
        long res = 1;
        while (b > 0){
            if ((b & 1) == 1) res = (res * a) % mod;
            a =  a * a % mod;
            b >>= 1;
        }
        return res;
    }

    static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    static Input f = new Input(System.in);

    static class Input {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public Input(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() throws IOException {

            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(reader.readLine());
            }
            return tokenizer.nextToken();
        }

        public String nextLine() throws IOException {
            String str = null;
            str = reader.readLine();
            return str;
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public Double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }
}

                                                                                                                                               

  • 25
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Studying~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值