1. 程式人生 > >Codeforces Round #524 (Div. 2)

Codeforces Round #524 (Div. 2)

A. Petya and Origami
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Petya is having a party soon, and he has decided to invite his n friends.

He wants to make invitations in the form of origami. For each invitation, he needs two red sheets, five green sheets, and eight blue sheets. The store sells an infinite number of notebooks of each color, but each notebook consists of only one color with k sheets. That is, each notebook contains k sheets of either red, green, or blue.

Find the minimum number of notebooks that Petya needs to buy to invite all n of his friends.

Input
The first line contains two integers n and k (1≤n,k≤108) — the number of Petya's friends and the number of sheets in each notebook respectively.

Output
Print one number — the minimum number of notebooks that Petya needs to buy.

Examples
inputCopy
3 5
outputCopy
10
inputCopy
15 6
outputCopy
38
Note
In the first example, we need 2 red notebooks, 3 green notebooks, and 5 blue notebooks.

In the second example, we need 5 red notebooks, 13 green notebooks, and 20 blue notebooks.

題解:petya有n個朋友,他想邀請他們,製作一個請帖需要2個紅紙片,5個綠的,8個藍的。一個筆記本上只有一種顏色,有k張紙片,問他總共需要購買幾個筆記本。

n*2就是需要的紅紙片個數,再除以k,再向上取整,就是需要這種顏色的筆記本的個數。三種顏色的個數加起來就是了。

#include <cstdio>
#include <cmath>

int main(){
    int n,k;
    while(~scanf("%d %d",&n,&k)){
        int sum=0;
        sum+=(int)ceil((n*2*1.0)/k);
        sum+=(int)ceil((n*5*1.0)/k);
        sum+=(int)ceil((n*8*1.0)/k);
        printf("%d\n",sum);


    }
}
View Code

 

B. Margarite and the best present time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

Little girl Margarita is a big fan of competitive programming. She especially loves problems about arrays and queries on them.

Recently, she was presented with an array aa of the size of 109109 elements that is filled as follows:

  • a1=1a1=−1
  • a2=2a2=2
  • a3=3a3=−3
  • a4=4a4=4
  • a5=5a5=−5
  • And so on ...

That is, the value of the ii-th element of the array aa is calculated using the formula ai=i(1)iai=i⋅(−1)i.

She immediately came up with qq queries on this array. Each query is described with two numbers: ll and rr. The answer to a query is the sum of all the elements of the array at positions from ll to rr inclusive.

Margarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to you — the best programmer.

Help her find the answers!

Input

The first line contains a single integer qq (1q1031≤q≤103) — the number of the queries.

Each of the next qq lines contains two integers ll and rr (1lr1091≤l≤r≤109) — the descriptions of the queries.

Output

Print qq lines, each containing one number — the answer to the query.

Example input Copy
5
1 3
2 5
5 5
4 4
2 3
output Copy
-2
-2
-5
4
-1
Note

In the first query, you need to find the sum of the elements of the array from position 11 to position 33. The sum is equal to a1+a2+a3=1+23=2a1+a2+a3=−1+2−3=−2.

In the second query, you need to find the sum of the elements of the array from position 22 to position 55. The sum is equal to a2+a3+a4+a5=23+45=2a2+a3+a4+a5=2−3+4−5=−2.

In the third query, you need to find the sum of the elements of the array from position 55 to position 55. The sum is equal to a5=5a5=−5.

In the fourth query, you need to find the sum of the elements of the array from position 44 to position 44. The sum is equal to a4=4a4=4.

In the fifth query, you need to find the sum of the elements of the array from position 22 to position 33. The sum is equal to a2+a3=23=1a2+a3=2−3=−1.

思路:題意很清楚了,有個陣列,每個數如那個公式所述。輸入l,r,求這一段中間的數字之和。

剛開始很容易想到挨著加起來,但是當輸入是 1 1e9的時候,就會超時,所以要找規律。

然後可以起點終點可以分別是相同的時候, 奇奇 奇偶 偶奇 偶偶 的時候,寫出公式即可。

 1 #include <stdio.h>
 2 
 3 int main(){
 4     int q,l,r;
 5     scanf("%d",&q);
 6     while(q--){
 7         scanf("%d %d",&l,&r);
 8         long long sum=0;
 9         if(l==r){
10             if(l%2==0){
11                 sum=l;
12             }else{
13                 sum=-1*l;
14             }
15         }
16         if(l%2==1&&r%2==1) sum=(r-l)/2+(-1*r);
17         if(l%2==1&&r%2==0) sum=(r+1-l)/2;
18         if(l%2==0&&r%2==0) sum=r-(r-l)/2;
19         if(l%2==0&&r%2==1) sum=-1*(r+1-l)/2;
20         /*for(int i=l;i<=r;i++){
21             if(i%2==0){
22                 sum+=i;
23             }else{
24                 sum+=(-1*i);
25             }
26         }*/
27         printf("%lld\n",sum);
28     }
29 }
View Code