Codeforces Round#196 前三題

果7發表於2013-08-17
A. Puzzles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces).

The shop assistant told the teacher that there are m puzzles in the shop, but they might differ in difficulty and size. Specifically, the first jigsaw puzzle consists of f1 pieces, the second one consists of f2 pieces and so on.

Ms. Manana doesn't want to upset the children, so she decided that the difference between the numbers of pieces in her presents must be as small as possible. Let A be the number of pieces in the largest puzzle that the teacher buys and B be the number of pieces in the smallest such puzzle. She wants to choose such n puzzles that A - B is minimum possible. Help the teacher and find the least possible value of A - B.

Input

The first line contains space-separated integers n and m (2 ≤ n ≤ m ≤ 50). The second line contains m space-separated integersf1, f2, ..., fm (4 ≤ fi ≤ 1000) — the quantities of pieces in the puzzles sold in the shop.

Output

Print a single integer — the least possible difference the teacher can obtain.

Sample test(s)
input
4 6
10 12 10 7 5 22
output
5
Note

Sample 1. The class has 4 students. The shop sells 6 puzzles. If Ms. Manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. It is impossible to obtain a smaller difference. Note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5.

                題目大意:很好懂,輸入n,m。給你m個數,讓你選n個數,讓裡面的最小值與最大值的差距最小,輸出最小差。

          解題思路:先對n個數排序,然後找相隔位置的差最小的。

          題目地址:A. Puzzles


AC程式碼:

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[1002];

int main()
{
    int n,m,i;
    scanf("%d%d",&n,&m);
    for(i=0;i<m;i++)
        scanf("%d",&a[i]);
    sort(a,a+m);
    int res=1000000;
    for(i=m-1;i>=n-1;i--)
        if(a[i]-a[i-n+1]<res)
           res=a[i]-a[i-n+1];
    cout<<res<<endl;
    return 0;
}


B. Routine Problem
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Manao has a monitor. The screen of the monitor has horizontal to vertical length ratio a:b. Now he is going to watch a movie. The movie's frame has horizontal to vertical length ratio c:d. Manao adjusts the view in such a way that the movie preserves the original frame ratio, but also occupies as much space on the screen as possible and fits within it completely. Thus, he may have to zoom the movie in or out, but Manao will always change the frame proportionally in both dimensions.

Calculate the ratio of empty screen (the part of the screen not occupied by the movie) to the total screen size. Print the answer as an irreducible fraction p / q.

Input

A single line contains four space-separated integers abcd (1 ≤ a, b, c, d ≤ 1000).

Output

Print the answer to the problem as "p/q", where p is a non-negative integer, q is a positive integer and numbers p and q don't have a common divisor larger than 1.

Sample test(s)
input
1 1 3 2
output
1/3
input
4 3 2 2
output
1/4
Note

Sample 1. Manao's monitor has a square screen. The movie has 3:2 horizontal to vertical length ratio. Obviously, the movie occupies most of the screen if the width of the picture coincides with the width of the screen. In this case, only 2/3 of the monitor will project the movie in the horizontal dimension:

Sample 2. This time the monitor's width is 4/3 times larger than its height and the movie's frame is square. In this case, the picture must take up the whole monitor in the vertical dimension and only 3/4 in the horizontal dimension:


           題目大意:給你螢幕長比寬,然後需要放的錄影的長比寬。可以任意收縮錄影大小,長寬比不能變,放進螢幕裡,問最小的空白比例是多少?

           解題思路:主要坑在了輸出0的情況,開始沒改這個然後以為是長寬可以變,然後就把長寬先處理了下,見程式碼。然後又WA,看了輸出0不能直接輸出0,需要輸出0/x. 最後被黑了,想不通就直接睡了,當時不應該加if(a<b) a-b交換的情況。 今天想了下,真的不能換,不然你的頭也要跟著轉方向了。。。

            說下我的思路吧!if(a/b>c/d)這說明d必須和b對齊,然後d就變成了b,c就變成了c*b/d,然後就直接是這個式子(a-c*b/d)/a直接變成(ad-bc)/ad.然後另一種情況也相同的討論,相同就輸出0/1 TAT

            題目地址:B. Routine Problem

AC程式碼:

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;

int gcd(int m,int n)
{
    int t;
    while(n)
    {
        t=m%n; m=n; n=t;
    }
    return m;
}
int main()
{
    int a,b,c,d;
    while(~scanf("%d%d%d%d",&a,&b,&c,&d))
    {
        /*if(a<b)  //因為多加了這個被黑了
        {
            int tmp=a;
            a=b;
            b=tmp;
        }
        if(c<d)
        {
            int tmp=c;
            c=d;
            d=tmp;
        }*/

        if((double)a/(double)b>(double)c/(double)d)
        {
            int p1=a*d-b*c;
            int p2=a*d;
            int q=gcd(p1,p2);
            p1=p1/q;
            p2=p2/q;
            printf("%d/%d\n",p1,p2);
        }
        else if((double)a/(double)b<(double)c/(double)d)
        {
            int p1=b*c-a*d;
            int p2=b*c;
            int q=gcd(p1,p2);
            p1=p1/q;
            p2=p2/q;
            printf("%d/%d\n",p1,p2);
        }
        else
        {
            printf("0/1\n");  //坑的地方
        }
    }
    return 0;
}

C. Quiz
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number k, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero.

Manao remembers that he has answered exactly m questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109 + 9).

Input

The single line contains three space-separated integers nm and k (2 ≤ k ≤ n ≤ 109; 0 ≤ m ≤ n).

Output

Print a single integer — the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109 + 9).

Sample test(s)
input
5 3 2
output
3
input
5 4 2
output
6
Note

Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points.

Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4.

Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000 mod 1000000009, even though2000000020 mod 1000000009 is a smaller number.


            題目大意:給你n個題,答對一個題目加1分,然後計數器加1,如果錯一個,計數器就變成了0.連續答對k個題,先加上這個題的1分,然後再乘上2,計數器清零。問給你一個m,k,求最少得到的分。

            解題思路:先把第k,2k,3k..這樣的位置全部答錯,意思就是沒有翻倍的情況。如果m比這樣所有的分加起來,就有翻倍的情況了。最後統計翻倍的和會發現是等比數列的和,需要用到快速冪。然後就是再加上後面沒有翻倍的,記住%m+m)%m。避免出現負數的情況。


           題目地址:C. Quiz

AC程式碼:

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<algorithm>
#define mo 1000000009
using namespace std;

__int64 fastmi(int p) //快速冪取模
{
    __int64 base=2;
    __int64 ans=1;
    while(p)
    {
        if(p&1)
            ans=(ans*base)%mo;
        base=(base*base)%mo;
        p>>=1;
    }
    return ans;
}

int main()
{
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        int score=(n/k)*(k-1)+n%k;  //第k,2k,3k...全部錯
        if(m<=score)
            printf("%d\n",m);
        else
        {
            __int64 p=m-score;
            __int64 res=((n/k-p)*(k-1)+n%k)%mo;  //後面的不翻倍情況
            p++;
            __int64 tmp=fastmi(p);
            tmp=(tmp-2)*k%mo;  //前面的翻倍情況
            res=((res+tmp)%mo+mo)%mo;
            printf("%I64d\n",res);
        }
    }
    return 0;
}

十二點cf才開始的,節奏有點亂了。。

       

相關文章