Codeforces Round #619 (Div. 2)C(构造,容斥)

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int main(){
 5     ios::sync_with_stdio(false);
 6     cin.tie(NULL);
 7     cout.tie(NULL);
 8     int t;
 9     cin>>t;
10     while(t--){
11         long long n,m;
12         cin>>n>>m;
13         long long ans=0;
14         ans=(n*(n+1))>>1;//总方案数(无论当中是否存在1)
15         long long temp=n-m;//0的个数
16         long long x=temp/(1+m);//将0均分(猜测这样答案最大)
17         long long y=temp%(1+m);//有y段会多余1个0,长度为x+1
18         long long z=1+m-y;//长度为x的段数
19         ans-=z*((x+1)*x/2)+y*((x+1)*(x+2)/2);//总方案数减去中间全是0的字串数量
20         cout<<ans<<"\n";
21     }
22     return 0;
23 }

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/12312950.html
今日推荐