题目描述
Several days ago you bought a new house and now you are planning to start a renovation. Since winters in your region can be very cold you need to decide how to heat rooms in your house.
Your house has nn rooms. In the ii -th room you can install at most c_ici heating radiators. Each radiator can have several sections, but the cost of the radiator with kk sections is equal to k^2k2 burles.
Since rooms can have different sizes, you calculated that you need at least sum_isumi sections in total in the ii -th room.
For each room calculate the minimum cost to install at most c_ici radiators with total number of sections not less than sum_isumi .
输入格式
The first line contains single integer nn ( 1 \le n \le 10001≤n≤1000 ) — the number of rooms.
Each of the next nn lines contains the description of some room. The ii -th line contains two integers c_ici and sum_isumi ( 1 \le c_i, sum_i \le 10^41≤ci,sumi≤104 ) — the maximum number of radiators and the minimum total number of sections in the ii -th room, respectively.
输出格式
For each room print one integer — the minimum possible cost to install at most c_ici radiators with total number of sections not less than sum_isumi .
数据范围
输入输出样例
输入 #1复制
4 1 10000 10000 1 2 6 4 6输出 #1复制
100000000 1 18 10说明/提示
In the first room, you can install only one radiator, so it's optimal to use the radiator with sum_1sum1 sections. The cost of the radiator is equal to (10^4)^2 = 10^8(104)2=108 .
In the second room, you can install up to 10^4104 radiators, but since you need only one section in total, it's optimal to buy one radiator with one section.
In the third room, there 77 variants to install radiators: [6, 0][6,0] , [5, 1][5,1] , [4, 2][4,2] , [3, 3][3,3] , [2, 4][2,4] , [1, 5][1,5] , [0, 6][0,6] . The optimal variant is [3, 3][3,3] and it costs 3^2+ 3^2 = 1832+32=18 .
题意翻译
有n个房间,每一个房间i最多可以装ci个暖气,温暖度至少为sumi,一个房间的温暖度为房间里所有暖气温暖度之和。
对于一个温暖度为k的暖气,需要花费 k^2 元,求对于每个房间,最少需要多少钱可以让子房间满足要求。
输入格式
第一行一个整数n,表示房间的总数
接下来n行,每行两个整数
第i行的两个整数分别表示ci,sumi输出格式
n行,每行一个整数表示第ii个房间所需的最少花费
解题目标:求每个房间的最小消费。
解题类型:模拟,贪心
解题思路:k温度耗费k^2,最小消费一定围绕sumi/ci波动。
1)判断当前 sumi能否整除ci, 若能直接 ans = (sumi/ci)^2*ci,继续下个房间。
2)否则,ans += (sumi/ci)^2;//sumi 和 ci都要变化下一次求最小是在新的sumi和ci的基础上。
sumi -= (sumi/ci);
同时修改 ci--;继续循环判断1)2)步。
代码:
#include <bits/stdc++.h>
#define MAXN 1e6+2
#define inf 0x3f3f3f3f
#define rep(x, a, b) for(int x=a; x<=b; x++)
#define per(x, a, b) for(int x=a; x>=b; x--)
using namespace std;
const int NC = 1e5+2;
int main()
{
int n;
int ci, sumi;
long long ans;
scanf("%d", &n);
rep(i, 1, n)
{
int temp;
scanf("%d%d", &ci, &sumi);
ans = 0;
while(ci)
{
if(sumi % ci != 0)
{
temp = sumi/ci;
sumi -= temp;
ans += temp*temp;
ci--;
}
else
{
temp = sumi /ci;
ans += temp*temp * ci;
ci = 0;
}
}
cout<<ans<<endl;
}
return 0;
}