Cf mongod newyearletter [DP + brute force]

Source: Internet
Author: User

Question:

K, X, n, m

Find the string S1, S2, S1 length N, S2 length m

Give rule, Sn = sn-1 + sn-2

Causes X "AC" substrings in SK

 

It is easy to know that there are several 12, several 21, several 22 sub-strings after K operations, several 1 strings, and several 2 strings.

If we know that there are several AC in S1 and several AC in S2

S1 head and tail, S2 head and tail

We can calculate the number

Now, in turn, we do not know S1, S2. We need to find such S1, S2

So we enumerate

S1 head and tail, S2 head and tail

Several AC appear in S1, and several AC appear in S2.

Check whether the numbers of AC and X in the calculated SK are equal.

Until such

If no, the output is happy new year.

 

#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>using namespace std;long long k,x,n,m;long long lastnum[4][4];long long num[4][4];long long x1=1,x2=0;long long xx1=0,xx2=1;long long tmp[4][4];void copy(long long to[][4],long long from[][4]){for(long long i=1;i<=2;i++)for(long long j=1;j<=2;j++)to[i][j]=from[i][j];}void add(long long to[][4],long long from[][4]){for(long long i=1;i<=2;i++)for(long long j=1;j<=2;j++)to[i][j]+=from[i][j];}long long ac_num(char* s){long long cnt=0;long long len=strlen(s);for(long long i=0;i<len;i++)if(s[i]=='A' && s[i]=='C') cnt++;return cnt;}bool can(long long num1,long long num2,long long suf1,long long suf2,long long pre1,long long pre2){if(xx1*num1+xx2*num2+num[1][2]*(suf1=='A' && pre2=='C')+num[2][2]*(suf2=='A' && pre2=='C')+num[2][1]*(suf2=='A' && pre1=='C')==x){///cout<<"n::"<<num[1][2]<<' '<<num[2][2]<<' '<<num[2][1]<<endl;return true;}elsereturn false;}bool print(long long num1,long long num2, char suf1,char suf2,char pre1,char pre2){        if(n==1 && pre1 != suf1) return false;        if(m==1 && pre2 != suf2) return false;if(n%2==0){ if(num1==n/2) if(!(suf1=='C' && pre1=='A')) return false;}else if(num1==n/2)if ((!(pre1=='A'||suf1=='C'))&&(n!=1))return false;if(m%2==0){ if(num2==m/2) if(!(suf2=='C' && pre2=='A')) return false;}else if(num2==m/2) if ((!(pre2=='A'||suf2=='C'))&&(m!=1)) return false;if(n%2==0){if(num1==n/2){for(long long i=1;i<=num1;i++)cout<<"AC";}else{cout<<pre1;for(long long i=1;i<=num1;i++)cout<<"AC";for(long long i=1;i<=n-num1*2-2;i++)cout<<"B";cout<<suf1;}}else{if(num1==n/2){            if(n==1) cout<<pre1;else                if(pre1=='A'){                    for(long long i=1;i<=num1;i++)cout<<"AC";                    cout<<suf1;                }else if(suf1=='C'){                    cout<<pre1;                    for(long long i=1;i<=num1;i++)cout<<"AC";                }}else{cout<<pre1;for(long long i=1;i<=num1;i++)cout<<"AC";for(long long i=1;i<=n-num1*2-2;i++)cout<<"B";cout<<suf1;}}cout<<endl;if(m%2==0){if(num2==m/2){for(long long i=1;i<=num2;i++)cout<<"AC";}else{cout<<pre2;for(long long i=1;i<=num2;i++)cout<<"AC";for(long long i=1;i<=m-num2*2-2;i++)cout<<"B";cout<<suf2;}}else{if(num2==m/2){            if(m==1) cout<<pre2;else                if(pre2=='A'){                    for(long long i=1;i<=num2;i++)cout<<"AC";                    cout<<suf2;                }else if(suf2=='C'){                    cout<<pre2;                    for(long long i=1;i<=num2;i++)cout<<"AC";                }}else{cout<<pre2;for(long long i=1;i<=num2-1;i++)cout<<"AC";for(long long i=1;i<=m-num2*2-2;i++)cout<<"B";cout<<suf2;}}cout<<endl;return true;}int main(){#ifndef ONLINE_JUDGEfreopen("G:/in.txt","r",stdin);#endifcin>>k>>x>>n>>m;for(long long i=3;i<=k;i++){long long xx1tmp=xx1,xx2tmp=xx2;xx1+=x1;xx2+=x2;x1=xx1tmp,x2=xx2tmp;copy(tmp,num);add(num,lastnum);copy(lastnum,tmp);if(i==3)        num[1][2]=1;if(i>=4 && i%2==0)num[2][1]++;else if(i>=4)num[2][2]++;}for(long long num1=0;num1<=(n/2);num1++)for(long long num2=0;num2<=(m/2);num2++)for(long long suf1='A';suf1<='C';suf1++)for(long long suf2='A';suf2<='C';suf2++)for(long long pre1='A';pre1<='C';pre1++)for(long long pre2='A';pre2<='C';pre2++)if(can(num1,num2,suf1,suf2,pre1,pre2)){                                                                        ///cout<<xx1<<' '<<xx2<<endl;//                                                                        cout<<num1<<' '//                                                                        <<num2<<' '//                                                                        <<(char)pre1<<' '//                                                                        <<(char)pre2<<' '//                                                                        <<(char)suf1<<' '//                                                                        <<(char)suf2<<' '<<endl;if(print(num1,num2,suf1,suf2,pre1,pre2)){return 0;                                                                }}cout<<"Happy new year!"<<endl;}


 

Cf mongod newyearletter [DP + brute force]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.