Codeforces Round #654 (Div. 2) 参与排名人数14349 本场比赛主要心思放在观摩高手如何打比赛,发现初中生真的很厉害。
[codeforces 1371B] Magical Calendar 看懂题意是关键+画图找规律
总目录详见https://blog.csdn.net/mrcrack/article/details/103564004
在线测评地址http://codeforces.com/contest/1371/problem/B
ProblemLangVerdictTimeMemoryB - Magical Calendar GNU C++17Accepted31 ms200 KB题目大意:给定一个k(1<=k<=r)列的表格,行数不限,给n个连续的格子染色,问染色的情况有几种?
该题最大的难度在于读懂题意,该题最难理解的一句,如下
Alice is going to paint some n consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day's cell is the leftmost cell in the next (under) row.
from the left cell to the right cell的left代表自左开始,请注意,未必是最左。
要理解好该句,请看部分样例模拟如下
3 4 4数据生成过程如下
k=1,有1种情况
k=2,有2种情况
k=3,有1种情况
总共有1+2+1=4种
13 7 28数据生成过程如下
k=1,有1种情况
k=2,有2种情况
k=3,有3种情况
k=4,有4种情况
k=5,有5种情况
k=6,有6种情况
k=7,有7种情况
总共有1+2+3+4+5+6+7=28种
有了样例模拟得基础,就容易讨论了
1<=k<=r,若n>k,需要两行起步,对应的种类有k种,
若n==k,只有一行,对应的种类有1种,
若n<k,只有一行,对应的种类有1种。
AC代码如下:
#include <stdio.h> #define LL long long LL ans; int main(){ int t,n,r,i,cnt; scanf("%d",&t); while(t--){ ans=0,cnt=0; scanf("%d%d",&n,&r); if(n>r)ans=(LL)(1+r)*r/2; else if(n==r)ans=(LL)(1+r-1)*(r-1)/2+1; else ans=(LL)(1+n-1)*(n-1)/2+1;//n<r printf("%lld\n",ans); } return 0; }