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 ",sum); } }
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!
The first line contains a single integer qq (1≤q≤1031≤q≤103) — the number of the queries.
Each of the next qq lines contains two integers ll and rr (1≤l≤r≤1091≤l≤r≤109) — the descriptions of the queries.
Print qq lines, each containing one number — the answer to the query.
5 1 3 2 5 5 5 4 4 2 3
-2 -2 -5 4 -1
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+2−3=−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=2−3+4−5=−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=2−3=−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 ",sum); 28 } 29 }