SSLOJ 1335.蛋糕切割【规律】

233

  • 题目:
  • 题意:
  • 分析:
  • 代码:


题目:

传送门


题意:

给出 n ∗ m n*m nm大小的一整块蛋糕,每小块为 1 ∗ 1 1*1 11
从对角线切去,一共能雨露均沾到几块小蛋糕


分析:

直接莽规律,开始手玩了近 1 h 1h 1h结果一直看不来什么
后来找了个画图网站,再试了下立马发现了 . . . ... ...
对于 2 ∗ 3 2*3 23 3 ∗ 2 3*2 32,我们发现本质其实是一样的,为了方便计算,我们将较小的作为第一项
2 ∗ 3 2*3 23 2 ∗ 2 2*2 22多了 2 2 2
3 ∗ 4 3*4 34 3 ∗ 3 3*3 33多了 3 3 3
4 ∗ 5 4*5 45 4 ∗ 4 4*4 44多了 4 4 4
以此类推,我们可以得到一个简单的规律,而 3 ∗ 5 3*5 35只比 3 ∗ 4 3*4 34 1 1 1,其他的也不尽相同
但如此我们计算 6 ∗ 9 6*9 69时得到的便是 14 14 14,实际应该是 12 12 12
我们再次画图,发现可以将 6 ∗ 9 6*9 69分成三个 2 ∗ 3 2*3 23,如此一来先计算 2 ∗ 3 2*3 23的答案,最后转化得到 6 ∗ 9 6*9 69的答案


代码:

#include
#include
#include
#include
#include
#include
#include
#define LL long long 
#define LZX Mu
using namespace std;
inline LL read() {
    LL d=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
    return d*f;
}
int main()
{
	int n=read(),m=read(),king=__gcd(n,m);
	if(n==m) return !printf("%d",n);
	if(n>m) swap(n,m);
	n/=king;m/=king;
	int ans=n<<1;
	for(int i=n+2;i<=m;i++,ans++);
	cout<<ans*king;
	return 0;
}

你可能感兴趣的:(规律)