• BZOJ_2081_[Poi2010]Beads_哈希


    BZOJ_2081_[Poi2010]Beads_哈希

    Description

    Zxl有一次决定制造一条项链,她以非常便宜的价格买了一长条鲜艳的珊瑚珠子,她现在也有一个机器,能把这条珠子切成很多块(子串),每块有k(k>0)个珠子,如果这条珠子的长度不是k的倍数,最后一块小于k的就不要拉(nc真浪费),保证珠子的长度为正整数。 Zxl喜欢多样的项链,为她应该怎样选择数字k来尽可能得到更多的不同的子串感到好奇,子串都是可以反转的,换句话说,子串(1,2,3)和(3,2,1)是一样的。写一个程序,为Zxl决定最适合的k从而获得最多不同的子串。 例如:这一串珠子是: (1,1,1,2,2,2,3,3,3,1,2,3,3,1,2,2,1,3,3,2,1), k=1的时候,我们得到3个不同的子串: (1),(2),(3) k=2的时候,我们得到6个不同的子串: (1,1),(1,2),(2,2),(3,3),(3,1),(2,3) k=3的时候,我们得到5个不同的子串: (1,1,1),(2,2,2),(3,3,3),(1,2,3),(3,1,2) k=4的时候,我们得到5个不同的子串: (1,1,1,2),(2,2,3,3),(3,1,2,3),(3,1,2,2),(1,3,3,2)

    Input

    共有两行,第一行一个整数n代表珠子的长度,(n<=200000),第二行是由空格分开的颜色ai(1<=ai<=n)。

    Output

    也有两行,第一行两个整数,第一个整数代表能获得的最大不同的子串个数,第二个整数代表能获得最大值的k的个数,第二行输出所有的k(中间有空格)。

    Sample Input

    21
    1 1 1 2 2 2 3 3 3 1 2 3 3 1 2 2 1 3 3 2 1

    Sample Output

    6 1
    2


    直接枚举答案,然后拿出 n/答案 个串进行哈希。

    然后扔map里去重。

    当前枚举的i*ans>n时就break。

    时间复杂度O(nlognlogn)

    代码:

    #include <cstdio>
    #include <string.h>
    #include <algorithm>
    #include <map>
    using namespace std;
    #define N 2000050
    typedef long long ll;
    map<int,int>H;
    inline char nc() {
        static char buf[100000],*p1,*p2;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    }
    int rd() {
        int x=0; char c=nc();
        while(c<'0'||c>'9') c=nc();
        while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=nc();
        return x;
    }
    const int mod=98754321,base=2333;
    int mi[N],h1[N],h2[N],ans[N];
    int n,a[N];
    int get_hash1(int l,int r) {
        return (h1[r]-(1ll*h1[l-1]*mi[r-l+1])%mod+mod)%mod;
    }
    int get_hash2(int l,int r) {
        return (h2[r]-(1ll*h2[l-1]*mi[r-l+1])%mod+mod)%mod;
    }
    struct A {
        int p,q;
    }b[N];
    bool cmp1(const A &x,const A &y) {return x.p<y.p;}
    bool cmp2(const A &x,const A &y) {return x.q<y.q;}
    int main() {
        n=rd();
        int i,mx=0,j,l;
        for(mi[0]=1,i=1;i<=n;i++) a[i]=rd(),mi[i]=1ll*mi[i-1]*base%mod,h1[i]=(1ll*h1[i-1]*base+a[i])%mod;
        for(i=1;i<=n;i++) h2[i]=(1ll*h2[i-1]*base+a[n-i+1])%mod;
        for(i=1;i<=n;i++) {
            H.clear();
            if(i*mx>n) break;
            l=0;
            for(j=1;j+i-1<=n;j+=i) {
                int t=j+i-1;
                int t1=get_hash1(j,t);
                int t2=get_hash2(n-t+1,n-j+1);
                // printf("%d %d
    ",b[l].p,b[l].q);
                if(!H[t1]||!H[t2]) {
                    l++;
                }
                H[t1]=1; H[t2]=1;
            }
            if(l>mx) ans[ans[0]=1]=i,mx=l;
            else if(l==mx) ans[++ans[0]]=i;
        }
        printf("%d %d
    ",mx,ans[0]);
        for(i=1;i<=ans[0];i++) {
            printf("%d ",ans[i]);
        }
    }
    
  • 相关阅读:
    org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 问题解决方法
    springboot入门(一)--快速搭建一个springboot框架
    SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
    理解Spring4.0新特性@RestController注解
    Intellij IDEA 搭建Spring Boot项目(一)
    Java checked 异常 和 RuntimeException(运行时异常)
    Java 如何抛出异常、自定义异常、手动或主动抛出异常
    微服务设计
    为什么需要API网关?
    微服务API网关
  • 原文地址:https://www.cnblogs.com/suika/p/9186846.html
Copyright © 2020-2023  润新知