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\n",(double)1);
           else printf("%.12lf\n",(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\n",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];
求这样的漂亮数的所有之和。
 对于这样题,分析起来其实还是很简单的, 对于求和(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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值