zoukankan      html  css  js  c++  java
  • CodeForces 379D 暴力 枚举

    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!

    题意:转自 http://www.cnblogs.com/wuminye/p/3500422.html

    【题目大意】

          告诉你初始字符串S1、S2的长度和递推次数k, 使用类似斐波纳契数列的字符串合并的递推操作,使得最后得到的字符串中刚好含有x个"AC",现在要你构造出满足条件的S1和S2。

    【分析】

          最终结果中有些"AC"可能是应为在合并时一个字符串的尾部和另一个字符串的首部合并而成,这就跟原始字符串的首尾字符有关,不同的情况在K次递推后多产生的"AC"数是不同的,所以这里既要枚举下初始串的首尾字符,计算出因合并产生的"AC"数sum有多少。

          现在可以忽略合并产生的"AC"了,假设S1中有a个"AC",S2中有b个"AC",则经过k次递推由这些"AC"组合成的"AC"数量为:fib[k-2]*a+fib[k-1]*b。

          所以最终的结果为:

             f[k]=fib[k-2]*a+fib[k-1]*b+sum;

         f[k]=x 已知,sum可以枚举获得 ,于是只需枚举a 即可知道 a和b 的值,对于一组 a,b值看能否构造出符合条件的字符串就好了。

         其实可以不用枚举a,用不定方程来解就好了,当a,b很大时速度更快。

      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdlib>
      4 #include<cstdio>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<queue>
      8 #include<map>
      9 
     10 #define N 105
     11 #define M 15
     12 #define mod 1000000007
     13 #define mod2 100000000
     14 #define ll long long
     15 #define maxi(a,b) (a)>(b)? (a) : (b)
     16 #define mini(a,b) (a)<(b)? (a) : (b)
     17 
     18 using namespace std;
     19 
     20 int k,n,m;
     21 ll f[N];
     22 int l[N];
     23 int r[N];
     24 ll z;
     25 ll x;
     26 void ini()
     27 {
     28     //k=0;
     29     //memset(next,0,sizeof(next));
     30    // memset(nexta,0,sizeof(nexta));
     31 }
     32 
     33 void solve()
     34 {
     35 
     36 }
     37 
     38 int main()
     39 {
     40     int i,j;
     41     int a,b,c,d;
     42     int o,p;
     43     //freopen("data.in","r",stdin);
     44    // scanf("%d",&T);
     45    // for(int cnt=1;cnt<=T;cnt++)
     46     //while(T--)
     47     while(scanf("%d%I64d%d%d",&k,&x,&n,&m)!=EOF)
     48     {
     49         for(i=0;i<=n/2;i++)
     50         {
     51             for(j=0;j<=m/2;j++)
     52             {
     53                 for(a=0;a<2;a++)
     54                 for(b=0;b<2;b++)
     55                 for(c=0;c<2;c++)
     56                 for(d=0;d<2;d++)
     57                 {
     58                     if (i * 2 + a + b > n||j * 2 + c + d > m)continue;
     59                     f[1]=i;f[2]=j;f[3]=i+j;l[2]=c;r[2]=d;
     60                     if(b==1 && c==1) f[3]++;
     61                     l[3]=a;r[3]=d;
     62                     for(z=4;z<=k;z++){
     63                         f[z]=f[z-1]+f[z-2];
     64                         l[z]=l[z-2];
     65                         r[z]=r[z-1];
     66                         if(r[z-2]==1 && l[z-1]==1) f[z]++;
     67                     }
     68 
     69                    // printf(" i=%d %d %d %d %d d=%d f=%I64d
    ",i,j,a,b,c,d,f[k]);
     70                     if(f[k]==x){
     71                         if(a==1) printf("C");
     72                         for(o=1;o<=i;o++){
     73                             printf("AC");
     74                         }
     75                         for(p=a+i*2;p<n-b;p++){
     76                             printf("M");
     77                         }
     78                         if(b==1) printf("A");
     79                         printf("
    ");
     80 
     81                         if(c==1) printf("C");
     82                         for(o=1;o<=j;o++){
     83                             printf("AC");
     84                         }
     85                         for(p=c+j*2;p<m-d;p++){
     86                             printf("M");
     87                         }
     88                         if(d==1) printf("A");
     89                         printf("
    ");
     90                         return 0;
     91                     }
     92                 }
     93             }
     94         }
     95         printf("Happy new year!
    ");
     96         //ini();
     97        // solve();
     98        // printf("%I64d
    ",ans);
     99     }
    100 
    101     return 0;
    102 }
  • 相关阅读:
    web服务之NginX介绍
    LVS介绍以及工作模式案例
    sersync 实现实时数据同步
    Java高并发20-并发包中锁原理解析(二)
    Java高并发19-并发包中锁原理解析(一)
    从零开始学VUE之VueRouter(导航守卫)
    从零开始学VUE之VueRouter(传递参数)
    从零开始学VUE之VueRouter(嵌套路由)
    从零开始学VUE之VueRouter(路由懒加载)
    从零开始学VUE之VueRouter(动态路由)
  • 原文地址:https://www.cnblogs.com/njczy2010/p/3933352.html
Copyright © 2011-2022 走看看