【20.23%】【codeforces 740A】Alyona and copybooks
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Little girl Alyona is in a shop to buy some copybooks for school. She study four subjects so she wants to have equal number of copybooks for each of the subjects. There are three types of copybook’s packs in the shop: it is possible to buy one copybook for a rubles, a pack of two copybooks for b rubles, and a pack of three copybooks for c rubles. Alyona already has n copybooks.
What is the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4? There are infinitely many packs of any type in the shop. Alyona can buy packs of different type in the same purchase.
Input
The only line contains 4 integers n, a, b, c (1 ≤ n, a, b, c ≤ 109).
Output
Print the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4.
Examples
input
1 1 3 4
output
3
input
6 2 1 1
output
1
input
4 4 4 4
output
0
input
999999999 1000000000 1000000000 1000000000
output
1000000000
Note
In the first example Alyona can buy 3 packs of 1 copybook for 3a = 3 rubles in total. After that she will have 4 copybooks which she can split between the subjects equally.
In the second example Alyuna can buy a pack of 2 copybooks for b = 1 ruble. She will have 8 copybooks in total.
In the third example Alyona can split the copybooks she already has between the 4 subject equally, so she doesn’t need to buy anything.
In the fourth example Alyona should buy one pack of one copybook.
【题目链接】:http://codeforces.com/problemset/problem/740/A
【题解】
题意:让你加上若干个1,2,3;然后使得n变成n+k;要求n+k能被4整除,并且数字1、2、3都有相应的价格,问价格最小是多少;
做法:
我是先用二分搞出比n大的第一个能被4整除的数字是多少->ans*4;
然后用ans*4-n;得到now;
然后根据now的大小分情况讨论;
now==0,不用数字输出0;
now==1,1个a、或b+c=5,或3个c->9;
now==2,1个b,两个a或2个c都行
now==3,3个a,||1个c,||a+b;
因为a,b,c大小可能很悬殊,所以几种情况都要比较;
【完整代码】
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 1e9;
LL n,a,b,c;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >> n >> a >> b >> c;
LL now = 0;
LL l = 0,r = MAXN,ans = 0;
while (l <= r)
{
LL m = (l+r)>>1;
if (4*m>=n)
{
ans = m;
r = m-1;
}
else
l = m+1;
}
if (4*ans == n)
puts("0");
else
{
LL now = ans*4-n;
LL temp = 0;
if (now==1)
{
temp = a;
temp = min(temp,b+c);
temp = min(temp,c*3);
}
else
if (now == 2)
{
temp = b;
temp = min(temp,a*2);
temp = min(temp,2*c);
}
else
if (now ==3)
{
temp = c;
temp = min(temp,a*3);
temp = min(temp,b+a);
}
cout << temp << endl;
}
return 0;
}
相关文章
- [CSS] Using single grid-template-area and justify-self to select arrow down icon in select
- [HTML5] Build Flexible HTML with HTMLTemplates using Slots and Web Components
- [Poi] Setup PostCSS and Tailwind with Poi
- [Spring Boot] Adding JPA and Spring Data JPA
- where and when navigation target url is retrieved by SAP UI5 Framework
- 一个好用的Chrome倒数计时器扩展 - Calendar and Countdown
- Atitit.ati orm的设计and架构总结 适用于java c# php版
- 【Codeforces Round #635 (Div. 2) A】Ichihime and Triangle
- 【77.39%】【codeforces 734A】Anton and Danik
- 【44.64%】【codeforces 743C】Vladik and fractions
- 【codeforces 761C】Dasha and Password(动态规划做法)
- 【codeforces 750C】New Year and Rating(做法2)
- 【codeforces 514B】Han Solo and Lazer Gun
- 【codeforces 46C】Hamsters and Tigers
- 【codeforces 743E】Vladik and cards
- TMS Cryptography Pack 4.2.X Delphi 10.3 Rio and Delphi 10.4 Sydney
- left_and_right_net可解释性设计
- Codeforces 450 C. Jzzhu and Chocolate
- Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2)