zoukankan      html  css  js  c++  java
  • CodeForces 379 D. New Year Letter

    枚举开头结尾的字母,枚举ac的个数,总AC个数就是两个Fibonacci数列的和。。。。。

    D. New Year Letter
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas).

    Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, s1 anf s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn - 2 + sn - 1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string sk on a piece of paper, puts it in the envelope and sends in to Santa.

    Vasya is absolutely sure that Santa will bring him the best present if the resulting string sk has exactly x occurrences of substring AC (the short-cut reminds him оf accepted problems). Besides, Vasya decided that string s1 should have length n, and string s2 should have length m. Vasya hasn't decided anything else.

    At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, s1 and s2 in the required manner. Help Vasya.

    Input

    The first line contains four integers k, x, n, m (3 ≤ k ≤ 50; 0 ≤ x ≤ 109; 1 ≤ n, m ≤ 100).

    Output

    In the first line print string s1, consisting of n uppercase English letters. In the second line print string s2, consisting of m uppercase English letters. If there are multiple valid strings, print any of them.

    If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.

    Sample test(s)
    input
    3 2 2 2
    
    output
    AC
    AC
    
    input
    3 3 2 2
    
    output
    Happy new year!
    
    input
    3 0 2 2
    
    output
    AA
    AA
    
    input
    4 3 2 1
    
    output
    Happy new year!
    
    input
    4 2 2 1
    
    output
    Happy new year!
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    
    using namespace std;
    
    int dp[120];
    char sa[12000],sb[12000];
    long long int fib[120];
    
    int getMAX(char s,char e,int l)
    {
    	if(l==1)
    	{
    		return 0;
    	}
    	if(l==2)
    	{
    		if(s=='A'&&e=='C') return 1;
    		else return 0;
    	}
    	if(l==3)
    	{
    		if(s=='A'||e=='C') return 1;
    		else return 0;
    	}
    	if(l>=4)
    	{
    		if(s=='A'&&e=='C') return (l-4)/2+2;
    		else if((s!='A'&&e=='C')||(s=='A'&&e!='C')) return (l-3)/2+1;
    		else return (l-2)/2;
    	}
    }
    
    long long int getfibK(int a,int b,int x)
    {
    	fib[0]=0;fib[1]=a;fib[2]=b;
    	for(int i=3;i<=x;i++)
    	{
    		fib[i]=fib[i-1]+fib[i-2];
    	}
    	if(fib[x]>1e9) return -1;
    	return fib[x];
    }
    
    
    
    int main()
    {
    	int k,x,n,m;
    	scanf("%d%d%d%d",&k,&x,&n,&m);
    	char s1,e1,s2,e2;
    	for(s1='A';s1<='C';s1++)
    	{
    		for(e1='A';e1<='C';e1++)
    		{
    		    if(n==1&&s1!=e1) continue;
    			for(s2='A';s2<='C';s2++)
    			{
    				for(e2='A';e2<='C';e2++)
    				{
    				    if(m==1&&s2!=e2) continue;
    					memset(dp,0,sizeof(dp));
    					char ls1=s1,le1=e1,ls2=s2,le2=e2;
    					for(int i=3;i<=k;i++)
    					{ 
    						dp[i]=dp[i-2]+(le1=='A'&&ls2=='C')+dp[i-1];
    						char ts=ls2;
    						ls2=ls1;
    						ls1=ts;le1=le2;
    					}
    					long long int tx=dp[k];
    					if(tx>x) continue;
    					int MaxS1=getMAX(s1,e1,n),MaxS2=getMAX(s2,e2,m);
    					for(int i=0;i<=MaxS1;i++)
    					{
    						for(int j=0;j<=MaxS2;j++)
    						{
    							long long int ty=getfibK(i,j,k);
    							if(ty<0) continue;
    							if(tx+ty==(long long int )x)
    							{
    								int posA=i,posB=j;
    								int cnta=1,cntb=1;
    								sa[0]=s1;sa[n-1]=e1;
    								sb[0]=s2;sb[m-1]=e2;
    								if(n!=1)
                                    {
                                        while(posA--)
                                        {
                                            if(cnta==1&&sa[0]=='A')
                                            {
                                                sa[cnta++]='C';
                                            }
                                            else
                                            {
                                                sa[cnta++]='A';
                                                sa[cnta++]='C';
                                            }
                                        }
                                        for(int ck=cnta;ck<n-1;ck++)
                                        {
                                            sa[ck]='B';
                                        }
                                    }
                                    if(m!=1)
                                    {
                                        while(posB--)
                                        {
                                            if(cntb==1&&sb[0]=='A')
                                            {
                                                sb[cntb++]='C';
                                            }
                                            else
                                            {
                                                sb[cntb++]='A';
                                                sb[cntb++]='C';
                                            }
                                        }
                                        for(int ck=cntb;ck<m-1;ck++)
                                        {
                                            sb[ck]='B';
                                        }
                                    }
    								printf("%s
    ",sa);
    								printf("%s
    ",sb);
    								return 0;
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    	printf("Happy new year!
    ");
    	return 0;
    }
    



  • 相关阅读:
    详解Linux 安装 JDK、Tomcat 和 MySQL(图文并茂)
    详解Linux 安装 JDK、Tomcat 和 MySQL(图文并茂)
    常见的面试C#技术题目
    Struts2中的ModelDriven机制及其运用
    Struts2 中的数据传输的几种方式
    Struts2 中的数据传输的几种方式
    form表单中method的get和post区别
    form表单中method的get和post区别
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/riasky/p/3507535.html
Copyright © 2011-2022 走看看