[Topcoder SRM 589 Division I, Level Three]FlippingBitsDiv1

傳送門

題解:
分塊暴力即可. 對於m小於n 暴力枚舉循環串然後dp .
對於m大於n 暴力枚舉每個循環串是否翻轉即可。
時間複雜度:O(2nn)

代碼:

#include<bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define ULL ull
#define mp make_pair
#define pii pair<int,int>
#define piii pair<int, pii >
#define pll pair <ll,ll>
#define pb push_back
#define big 20160116
#define INF 2147483647
#define pq priority_queue
#define rank rk124232
#define y1 y20160116
#define y0 y20160110
using namespace std;
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
namespace Mymath{
    LL qp(LL x,LL p,LL mod){
        LL ans=1;
        while (p){
            if (p&1) ans=ans*x%mod;
            x=x*x%mod;
            p>>=1;
        }
        return ans;
    }
    LL inv(LL x,LL mod){
        return qp(x,mod-2,mod);
    }
    LL C(LL N,LL K,LL fact[],LL mod){
        return fact[N]*inv(fact[K],mod)%mod*inv(fact[N-K],mod)%mod;
    }
    template <typename Tp> Tp gcd(Tp A,Tp B){
        if (B==0) return A;
        return gcd(B,A%B);
    }
    template <typename Tp> Tp lcm(Tp A,Tp B){
        return A*B/gcd(A,B);
    }
};
namespace fwt{
    using namespace Mymath;
    void FWT(int a[],int n,LL mod)
    {
        for(int d=1;d<n;d<<=1)
            for(int m=d<<1,i=0;i<n;i+=m)
                for(int j=0;j<d;j++)
                {
                    int x=a[i+j],y=a[i+j+d];
                    a[i+j]=(x+y)%mod,a[i+j+d]=(x-y+mod)%mod;
                    //xor:a[i+j]=x+y,a[i+j+d]=x-y;
                    //and:a[i+j]=x+y;
                    //or:a[i+j+d]=x+y;
                }
    }

    void UFWT(int a[],int n,LL mod)
    {
        LL rev=inv(2,mod);
        for(int d=1;d<n;d<<=1)
            for(int m=d<<1,i=0;i<n;i+=m)
                for(int j=0;j<d;j++)
                {
                    int x=a[i+j],y=a[i+j+d];
                    a[i+j]=1LL*(x+y)*rev%mod,a[i+j+d]=(1LL*(x-y)*rev%mod+mod)%mod;
                    //xor:a[i+j]=(x+y)/2,a[i+j+d]=(x-y)/2;
                    //and:a[i+j]=x-y;
                    //or:a[i+j+d]=y-x;
                }
    }
    void solve(int a[],int b[],int n,LL mod)
    {
        FWT(a,n,mod);
        FWT(b,n,mod);
        for(int i=0;i<n;i++) a[i]=1LL*a[i]*b[i]%mod;
        UFWT(a,n,mod);
    }
};
string s;
int dp[305][2];
int cnt[305][2];
class FlippingBitsDiv1{
    public:
        int getmin(vector<string> S,int M){
            for (int i=0;i<S.size();i++){
                s+=S[i];
            }
            int n=s.size();
            int ret=1e9;
            vector<string> ss;
            while (s.size()>M){
                ss.pb(s.substr(0,M));
                s=s.substr(M);
            }
            ss.pb(s);
            //for (int i=0;i<ss.size();i++) cout<<ss[i]<<endl;
            if (M<=18){
                for (int i=0;i<(1<<M);i++){
                    string t;
                    for (int j=0;j<M;j++){
                        if(i>>j&1) t+='1';
                        else t+='0';
                    }
                    int tt=0;
                    vector<pair<int,int> > V;
                    V.pb(mp(0,0));
                    for (int j=0;j<ss.size();j++){
                        int c1=0,c2=0;
                        for (int k=0;k<ss[j].size();k++){
                            if (t[k]!=ss[j][k]) c1++;
                            else c2++;
                        }
                        V.pb(mp(c1,c2));
                        //cout<<j<<' '<<c1<<' '<<c2<<endl;
                    }
                    for (int j=0;j<=ss.size();j++){
                        dp[j][0]=dp[j][1]=1e9;
                    }
                    dp[0][0]=0;
                    dp[0][1]=1;
                    for (int j=1;j<=ss.size();j++){
                        dp[j][0]=min(dp[j-1][0]+V[j].first,dp[j-1][1]+V[j].first);
                        dp[j][1]=min(dp[j-1][0]+V[j].second+2,dp[j-1][1]+V[j].second);
                    }
                    ret=min(ret,min(dp[ss.size()][0],dp[ss.size()][1]));
                }
                return ret;
            }
            int L=ss.size();
            for (int i=0;i<(1<<L);i++){
                //cout<<i<<endl;
                string t;
                for (int j=0;j<L;j++){
                    if(i>>j&1) t+='1';
                    else t+='0';
                }
                t+='0';
                int cc=0;
                for (int j=0;j<L;j++){
                    if (t[j]!=t[j+1]) cc++;
                }
                //cout<<t<<' '<<cc<<endl;
                for (int j=0;j<=M;j++) cnt[j][0]=cnt[j][1]=0;
                for (int j=0;j<L;j++){
                    for (int k=0;k<ss[j].size();k++){
                        int v=ss[j][k]-'0';
                        //cout<<v<<endl;
                        cnt[k][v^((i>>j)&1)]++;
                    }
                }
                //cerr<<132<<endl;
                int res=cc;
                for (int j=0;j<M;j++){
                    res+=min(cnt[j][0],cnt[j][1]);
                }
                ret=min(ret,res);
            }
            return ret;
        }
}tst;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章