Codeforces 758C. Unfair Poll

题目大意:

在一个(n*m)的棋盘上连续进行k次操作,每次操作会把格子里的数+1,初始为零.操作的顺序见题解.问最大值,最小值以及某一格子上的数。(n,mleq100,kleq10^{18})

题解:

由于操作的时候对于行是:(1,2,3,..,n,n-1,n-2,...,1,2,3,...)
对于列是:(1,2,3,...,n,1,2,3,...)
所以我们把(1..n..2)视作一个周期,把(k)取模即可.

注:注意n = 1的情况

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long ll;
template<typename T>inline void read(T &x){
	x=0;char ch;bool flag = false;
	while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
	while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
template<typename T>inline T cat_max(const T &a,const T &b){return a>b ? a:b;}
template<typename T>inline T cat_min(const T &a,const T &b){return a<b ? a:b;}
const ll maxn = 1024;
ll a[maxn][maxn];
int main(){
	ll n,m,x,y,k,mod,base;
	read(n);read(m);read(k);read(x);read(y);
	if(n == 1) mod = m;
	else mod = (2*n-2)*m;
	base = k/mod;k%=mod;
//	printf("%I64d %I64d
",k,base);
	for(ll i=1;i<=m;++i) a[1][i] = base,a[n][i] = base;
	for(ll i=2;i<n;++i){
		for(ll j=1;j<=m;++j){
			a[i][j] = base<<1LL;
		}
	}
	for(ll i=1;i<=n && k;++i){
		for(ll j=1;j<=m && k;++j){
			--k;
			a[i][j]++;
		}
	}
	for(ll i=n-1;i>=2 && k;--i){
		for(ll j=1;j<=m && k;++j){
			--k;
			a[i][j]++;
		}
	}
	ll ans1 = 0,ans2 = 1LL<<60;
	for(ll i=1;i<=n;++i){
		for(ll j=1;j<=m;++j){
	//		printf("%I64d ",a[i][j]);
			ans1 = cat_max(ans1,a[i][j]);
			ans2 = cat_min(ans2,a[i][j]);
		}//puts("");
	}
	printf("%I64d %I64d %I64d
",ans1,ans2,a[x][y]);
	getchar();getchar();
	return 0;
}


原文地址:https://www.cnblogs.com/Skyminer/p/6357684.html