Codeforces1260A Heating

思路:

1.因为需要cost最小,所以所有的暖气需要是sum/c(设为a)个部分或者sum/c+1(设为b)个部分;
2.我们设前者x个,后者y个,可以得到
{ax+by=sumx+y=c egin{cases} ax+by=sum\ x+y=c end{cases}
解出x、y然后算总开销即可;

代码:

#define IOS ios::sync_with_stdio(false);cin.tie(0)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){
	IOS;
	int n;
	cin>>n;
	while(n--){
		LL c,sum;
		cin>>c>>sum;
		LL a=sum/c,b=a+1;
		LL x=b*c-sum;
		LL y=c-x;
		cout<<x*a*a+y*b*b<<'
';	
	}
	return 0;
}
原文地址:https://www.cnblogs.com/yuhan-blog/p/12308811.html