zoukankan      html  css  js  c++  java
  • Codeforces Round #272 (Div. 2)

    A. Dreamoon and Stairs
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Dreamoon wants to climb up a stair of n steps. He can climb 1 or 2 steps at each move. Dreamoon wants the number of moves to be a multiple of an integer m.

    What is the minimal number of moves making him climb to the top of the stairs that satisfies his condition?

    Input

    The single line contains two space separated integers nm (0 < n ≤ 10000, 1 < m ≤ 10).

    Output

    Print a single integer — the minimal number of moves being a multiple of m. If there is no way he can climb satisfying condition print  - 1instead.

    Sample test(s)
    input
    10 2
    
    output
    6
    
    input
    3 5
    
    output
    -1
    
    Note

    For the first sample, Dreamoon could climb in 6 moves with following sequence of steps: {2, 2, 2, 2, 1, 1}.

    For the second sample, there are only three valid sequence of steps {2, 1}, {1, 2}, {1, 1, 1} with 2, 2, and 3 steps respectively. All these numbers are not multiples of 5.

    看上去非常复杂,事实上好水啊,

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int n,m;
        while(cin>>n>>m)
        {
            if(n < m)
            {
                cout<<"-1"<<endl;
                continue;
            }
            int Max = n/m*m;//最大最大不会超过这个
            bool flag = 0;
            int ans = -1;
            for(int i = m; i <= Max; i  += m)
            {
                    if(i <= n && 2*i >= n)
                    {
                        ans = i;
                        break;
                    }
            }
            cout<<ans<<endl;
        }
        return 0;
    }

    B. Dreamoon and WiFi
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them.

    Each command is one of the following two types:

    1. Go 1 unit towards the positive direction, denoted as '+'
    2. Go 1 unit towards the negative direction, denoted as '-'

    But the Wi-Fi condition is so poor that Dreamoon's smartphone reports some of the commands can't be recognized and Dreamoon knows that some of them might even be wrong though successfully recognized. Dreamoon decides to follow every recognized command and toss a fair coin to decide those unrecognized ones (that means, he moves to the 1 unit to the negative or positive direction with the same probability 0.5).

    You are given an original list of commands sent by Drazil and list received by Dreamoon. What is the probability that Dreamoon ends in the position originally supposed to be final by Drazil's commands?

    Input

    The first line contains a string s1 — the commands Drazil sends to Dreamoon, this string consists of only the characters in the set {'+''-'}.

    The second line contains a string s2 — the commands Dreamoon's smartphone recognizes, this string consists of only the characters in the set {'+''-''?'}. '?

    ' denotes an unrecognized command.

    Lengths of two strings are equal and do not exceed 10.

    Output

    Output a single real number corresponding to the probability. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9.

    Sample test(s)
    input
    ++-+-
    +-+-+
    
    output
    1.000000000000
    
    input
    +-+-
    +-??

    output
    0.500000000000
    
    input
    +++
    ?

    ?

    -

    output
    0.000000000000
    
    Note

    For the first sample, both s1 and s2 will lead Dreamoon to finish at the same position  + 1.

    For the second sample, s1 will lead Dreamoon to finish at position 0, while there are four possibilites for s2: {"+-++""+-+-""+--+","+---"} with ending position {+2, 0, 0, -2} respectively. So there are 2 correct cases out of 4, so the probability of finishing at the correct position is 0.5.

    For the third sample, s2 could only lead us to finish at positions {+1, -1, -3}, so the probability to finish at the correct position  + 3 is 0.


    我用dfs写的,首先统计由多少个?

    ,每一个?能够有两种选择,那么N个?

    就有2^N次方种可


    能,因为题目说n<=10,那么我们能够用dfs搜出全部由n个?

    组成的结果,最多最多仅仅有


    2^10=1024种,開始dfs写歪了了...
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int d[20000],p = 0;
    int sum,temp,c = 0;
    
    void dfs(int x, int deep)
    {
        c += x;
        if(deep == sum)
        {
            d[p++] = c;
            c -= x;
            return;
        }
        dfs(1,deep+1);
        dfs(-1,deep+1);
        c -= x;
    }
    int main()
    {
        #ifdef xxz
        freopen("in.txt","r",stdin);
        #endif // xxz
        string s1,s2;
        while(cin>>s1>>s2)
        {
           int len_s1 = s1.length();
           int len_s2 = s2.length();
           int pos = 0;
           for(int i = 0; i < len_s1; i++)
           {
               if(s1[i] == '+') pos++;
               else pos--;
           }
           sum = 0,temp = 0;
           for(int i = 0; i < len_s2; i++)
           {
               if(s2[i] == '+') temp++;
               else if(s2[i] == '-') temp--;
               else sum++;
           }
    
           p = 0;
           c= 0;
           if(sum == 0)
           {
               if(temp == pos) printf("%.12lf
    ",(double)1);
               else printf("%.12lf
    ",(double)0);
               continue;
           }
           else if(sum != 0)
           {
               dfs(1,1);
               c = 0;
               dfs(-1,1);
           }
           int count = 0;
           for(int i = 0; i < p; i++)
           {
               if(d[i] + temp == pos) count++;
           }
           printf("%.12lf
    ",count*1.0/p);
        }
        return 0;
    }

    C. Dreamoon and Sums
    time limit per test
    1.5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if  and , where k is some integer number in range[1, a].

    By  we denote the quotient of integer division of x and y. By  we denote the remainder of integer division of x and y. You can read more about these operations here: http://goo.gl/AcsXhT.

    The answer may be large, so please print its remainder modulo 1 000 000 007 (109 + 7). Can you compute it faster than Dreamoon?

    Input

    The single line of the input contains two integers ab (1 ≤ a, b ≤ 107).

    Output

    Print a single integer representing the answer modulo 1 000 000 007 (109 + 7).

    Sample test(s)
    input
    1 1
    
    output
    0
    
    input
    2 2
    
    output
    8
    
    Note

    For the first sample, there are no nice integers because  is always zero.

    For the second sample, the set of nice integers is {3, 5}.

    题意:

     给你两个整数a,b ,定义这样一个数为美丽数: 
     (1) x>0  ;  (2)x%b!=0 ;  (3)  (x/b)/(x%b)=k;  (4)k属于[1,a];
    求这种美丽数的全部之和。
     对于这样题。分析起来事实上还是非常easy的。 对于求和(sum),我们须要做的第一件事就是确定美丽数的边界(上限和下限)、因为(3) (x/b)/(x%b)=k
    能够得出: x/b = k*(x%b);              x%b的剩余系即{1。2,3,4,5,......b-1}里的最大系;我们不放设定y={1,2,3,4,5,........b-1};
    不难得出:  x=k*y*b+y; -->x=(k*b+1)*y 所以能够判断公式:  x=Σ1a (k*b+1)*Σ1b-1 (y);
     进一步简化后部分: x=Σ1a (k*b+1)*(b-1)*(b)/2;

    以后题目出现超int的,全部数据最好都定义longlong,要不然莫名其妙的wa....

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #define LL  long long
    const int MOD = 1e9+7;
    using namespace std;
    
    int main()
    {
        LL a,b;
        while(cin>>a>>b)
        {
            LL y = ((1+b-1)*(b-1)/2)%MOD;
            LL ans = 0;
            for(LL i = 1; i <= a; i++)
            {
                ans += ((i*b)%MOD +1)%MOD;
                ans %= MOD;
            }
    
            ans = (ans*y)%MOD;
            cout<<ans<<endl;
        }
    }

    D. Dreamoon and Sets
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Dreamoon likes to play with sets, integers and  is defined as the largest positive integer that divides both a and b.

    Let S be a set of exactly four distinct integers greater than 0. Define S to be of rank k if and only if for all pairs of distinct elements sisj fromS.

    Given k and n, Dreamoon wants to make up n sets of rank k using integers from 1 to m such that no integer is used in two different sets (of course you can leave some integers without use). Calculate the minimum m that makes it possible and print one possible solution.

    Input

    The single line of the input contains two space separated integers nk (1 ≤ n ≤ 10 000, 1 ≤ k ≤ 100).

    Output

    On the first line print a single integer — the minimal possible m.

    On each of the next n lines print four space separated integers representing the i-th set.

    Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal m, print any one of them.

    Sample test(s)
    input
    1 1
    
    output
    5
    1 2 3 5
    
    input
    2 2
    
    output
    22
    2 4 6 22
    14 18 10 16
    
    Note

    For the first example it's easy to see that set {1, 2, 3, 4} isn't a valid set of rank 1 since .

    1-m中。四个数凑成一组,满足随意2个数的gcd=k,求一个最小的m使得凑成n组解。

    并输出。

    非常自然,把k先无论,那么xi/k就得互质,于是

    1 2 3 4 5 6 7 8 9 10 11 12 13.。。。。。

    。。。。

    互质的一组是 1 2 3 5、4 7 11 13、、、、

    可是。1 2 3 5、7 8 9 11也是的,同一时候这组的解显然更优。由于数拉的不是非常大。于是,于是。

    1 2 3 5

    7 8 9 11

    13 14 15 17


    ......

    以下附上YYN巨巨的思路:

    本题的目标是选择4n个数填入到n行,每行四个数。要求是使得每行中的四个数两两的最大公约数等于K。问如何选择使得选择的最大的数字最小,而且要求输出这个最大的数以及每一行所填的数字。


    那么我们转化一下,将全部的数除以K。

    问题即转化成使得每行四个数两两互质。
    易知,每一行最多一个偶数,由于偶数之间不互质。


    如今我们如果每一行都有一个偶数,那么至少我们须要选择3n个奇数。如果我们选择最小的3n个奇数,那么最大的数即6n-1(我们如果偶数不会大于最大的奇数,其实最大的数一定是奇数。接下来我们会给出证明),每少一个偶数,则最大的数的大小便添加2。
    好了,如今我们如果仅仅用最小的3n个奇数以及n个均不大于6n-1的偶数能够满足题目要求!
    那么,第一行的答案就是6n-1。
    接下来n行我们以以下的形式构造:
    1 2 3 5
    7 8 9 11
    ……
    6i-5 6i-4 6i-3 6i-1
    ……
    6n-5 6n-4 6n-3 6n-1

    这样便得到了终于的答案。
    以这种构造方法交上去便可AC。

    可是我们是否能证明呢?显然是能够的。


    对于当中的某一行我们有i i+1 i+2 i+4(i为6x-5。当中x为正整数,同一时候我们能够知道i为正整数且i为奇数)。
    因为i。i+1。i+2两两互质。
    i和i+2不互质当且仅当i,i+2为偶数。但由上面的定义我们可知i必然为奇数,所以i。i+1,i+2两两互质。
    同理i+2,i+4也互质。
    所以我们仅仅须要i,i+4互质且i+1,i+4互质就可以。


    由于i与i+4不互质当且仅当i为4的倍数,i+1与i+4不互质当且仅当i+1为3的倍数。


    由于i为奇数,所以i必然与i+4互质。


    由于i+1 = 6x-4 = 2*(3x-2),不是3的倍数。所以i+1与i+4互质。


    因此。这种构造是可行且最小的。


    orz....

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    using namespace std;
    
    int main()
    {
        int n,k;
        while(cin>>n>>k)
        {
            int ans = (6*n-1)*k;
            int cent = -5;
            cout<<ans<<endl;
            for(int i = 1; i <= n; i ++)
            {
                cent += 6;
                cout<<cent*k<<" "<<(cent+1)*k<<" "<<(cent+2)*k<<" "<<(cent+4)*k<<endl;
            }
        }
    }
    // 1 2 3 5
    // 7 8 9 11
    // 13 14 15 17
    // 19 20 21 23


  • 相关阅读:
    govalidator----结构体tag验证
    结构字段验证--validator.v9
    序列化
    案例:8,64,256都是2的阶次方数(8是2的3次方),用Java编写程序来判断一个整数是不是2的阶次方数。
    易错点
    什么是线程与进程?
    对象与实例的区别?
    什么情况下用断言?assert
    垃圾收集器什么时候回收垃圾?
    HashMap 和 HashTable 的区别
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5186355.html
Copyright © 2011-2022 走看看