题目
The famous store "Second Food" sells groceries only two days a month. And the prices in each of days differ. You wanted to buy n� kilos of potatoes for a month. You know that on the first day of the month 11 kilo of potatoes costs a� coins, and on the second day b� coins. In "Second Food" you can buy any integer kilograms of potatoes.
Fortunately, "Second Food" has announced a promotion for potatoes, which is valid only on the first day of the month — for each m� kilos of potatoes you buy, you get 11 kilo as a gift! In other words, you can get m+1�+1 kilograms by paying for m� kilograms.
Find the minimum number of coins that you have to spend to buy at least n� kilos of potatoes.
Input
Each test contains multiple test cases. The first line contains the number of test cases t� (1≤t≤100001≤�≤10000). Description of the test cases follows.
The first line of each test case contains two integers a� and b� (1≤a,b≤109)(1≤�,�≤109) — the prices of 11 kilo of potatoes on the first and second days, respectively.
The second line contains two integers n� and m� (1≤n,m≤109)(1≤�,�≤109) — the required amount of potatoes to buy and the amount of potatoes to use the promotion.
Output
For each test case print one integer — the minimum number of coins that you have to pay to buy at least n� kilos of potatoes.
题意:
要在两天之内购买N件商品,第一天每件商品的单价为a,并且有活动每购买M件送1件,第二天商品单价为b.求购买N件商品的最低价
思路:
先判断买M送1的活动能不能用上,比较n和m-1的大小,不能用上比较a b 的大小直接算出来,
能用判断a与b的大小,a<=b,全在第一天购买,否则,比较在第一天买凑买m赠1剩下的第二天买,和全在第二天买的价格多少
代码:
#include<iostream>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
long long a,b,n,m;
long long s1=0,s2=0;
cin >> a >> b>> n >>m;
if(m+1>n){
s1=a*n;
s2=b*n;
if(s1<s2) cout << s1 << endl;
else cout << s2 << endl;
}
else {
if(a<=b) s1=m*a*(n/(m+1))+(n%(m+1))*a,cout << s1<< endl;
else {
s1=b*n,s2=m*a*(n/(m+1))+(n%(m+1))*b;
if(s1<s2) cout << s1 << endl;
else cout << s2 << endl;
}
}
}
}