Mr. Panda and Mrs. Panda is bored of the hustle and bustle of city life. They decide to make a change. They drop everything they belong to and move to a place where they can find real connections with people and nature. Here in Stardew Valley, they start their life as farmers. They are now embarking on tasks of reclaiming wastelands, sowing seeds, and planting trees.
They are now looking for a rectangle area from the reclaimed wasteland to cultivate their first crop. To prevent crops being damaged by annoying crows, they place several scarecrows inside this rectangle area. The scarecrows occupy a rectangle area that is surrounded by the crops. The wasteland is of N
rows and M columns. They wonder how many different ways to pick a rectangle area and place crops and scarecrows inside the rectangle. As the number can be large, return the answer modulo 109+7
.
Input
The first line of input gives the number of test cases T
(1≤T≤105). T test cases follow. Each test case starts with a line consisting of two integers N, M (1≤N,M≤105
), the number of rows and columns of the wasteland.
Output
For each test case, output one line containing "Case x: y", where x is the test case number (starting from 1
) and y is the number of different ways to place the crops and scarecrows, modulo 109+7
.
Sample Input
Input
3 2 3 3 3 4 4
Output
Case 1: 0 Case 2: 1 Case 3: 25
Hint
For test case 1, the wasteland is too small to place any scarecrows.
For test case 2, 3×3
is the minimal rectangle to place one scarecrow surrounded by 8 crops.
For test case 3, a 4×4
rectangle has 9 ways to place crops and scarecrows: 4 ways for 1×1 scarecrow, 2 ways for 1×2 scarecrows, 2 ways for 2×1 scarecrows, and 1 way for 2×2 scarecrows. Similarly, two 3×4 rectangles has 2×3=6 ways; two 4×3 rectangles has 2×3=6 ways, four 3×3 rectangles has 4×1=4 ways. In total, 9+6+6+4=25 ways.
思路:
从行选四个点,两点作为大矩形行边界,两点作为子矩形行边界,还可以选三个点,两个点作为大矩形行边界,一个点作为子矩形行边界,所以总共有c(n,4)+c(n,3)种选法,列也是这样,然后行和列乘起来,就是(c(n,4)+c(n,3))*(c(m,4)+c(m,3))
#include<stdio.h>
#include<algorithm>
//#include<string.h>
#include<iostream>
using namespace std;
#define mod 1000000007
#define ll long long
#define maxn 105000
#define p 1000000007
//#include<string>
long long f[100005];
long long g[100005];
ll qpow(ll x,ll n){
ll ans=1;
while(n){
if(n&1) ans=(ans*x)%mod;
x=x*x%mod;
n>>=1;
}
return ans;
}
ll inv(ll x){
return qpow(x,mod-2);
}
int main()
{
ll t,cas=0;
scanf("%lld",&t);
while(t--)
{
ll n,m;
scanf("%lld %lld",&n,&m);
if(n<3||m<3)
{
printf("Case %lld: 0\n",++cas);
continue;
}
//ll x=Lucas(n,4)+Lucas(n,3),y=Lucas(m,4)+Lucas(m,3);
ll x=0,y=0;
x=n%mod*(n-1)%mod*(n-2)%mod*(n-3)%mod*inv(1)%mod*inv(2)%mod*inv(3)%mod*inv(4)%mod+n%mod*(n-1)%mod*(n-2)%mod*inv(1)%mod*inv(2)%mod*inv(3)%mod;
y=m%mod*(m-1)%mod*(m-2)%mod*(m-3)%mod*inv(1)%mod*inv(2)%mod*inv(3)%mod*inv(4)%mod+m%mod*(m-1)%mod*(m-2)%mod*inv(1)%mod*inv(2)%mod*inv(3)%mod;
//printf("x=%lld y=%lld\n",x,y);
printf("Case %lld: %lld\n",++cas,(x%mod)*(y%mod)%mod);
//printf("Case %lld: %lld\n",++cas,(x%mod)*(y%mod)%mod);
//printf("对拍:%lld\n",js(n,m));
}
}