思维,就是反过来想,题解太强了
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin>>t; while(t--) { long long n,m; scanf("%lld%lld",&n,&m); if(m==0) { cout<<0<<endl; continue; } if(n==m) { cout<<n*(n+1)/2<<endl; continue; } long long ans=(n+1)*n/2; long long noz=n-m; if(noz-1<=m) { ans-=noz; cout<<ans<<endl; continue; } long long num =noz/(m+1);//每部分0的个数 long long res_group=noz%(m+1); ans=ans-(m+1-res_group)*num*(num+1)/2-res_group*(num+1)*(num+2)/2; printf("%lld ",ans); } return 0; }