题目描述
给定一个 𝑛∗𝑚n∗m 的棋盘,你需要在棋盘上放置黑白两个不同的皇后,请问有多少种放置方法能够使两个皇后之间互相不能攻击对方?
象棋中的皇后可以沿所在行、列及对角线移动任意距离。
输入格式
输入共一行,两个正整数 𝑛,𝑚n,m
输出格式
输出共一行,一个正整数表示答案
数据范围
- 对于 30%30% 的数据,2≤𝑛,𝑚≤1022≤n,m≤102
- 对于 60%60% 的数据,2≤𝑛,𝑚≤1042≤n,m≤104
- 对于 100%100% 的数据,2≤𝑛,𝑚≤101002≤n,m≤10100
样例数据
输入:
4 3
输出:
40
详见代码:
#include <bits/stdc++.h>
using namespace std;
string n,m;
bool bj(string x,string y)
{
int lenx=x.length();
int leny=y.length();
if (lenx>leny)
{
return true;
}
else if (lenx<leny)
{
return false;
}
else
{
for(int i=0;i<lenx;i++)
{
if (x[i]>y[i])
{
return true;
}
else if (x[i]<y[i]){
return false;
}
}
}
return true;
}
string jia(string x,string y)
{
string ret="";
int yy=0;
if (bj(x,y)==true)
{
swap(x,y);
}
int lenx=x.length();
int leny=y.length();
for(int i=lenx-1,j=leny-1;i>=0||j>=0;i--,j--)
{
if(i>=0)
{
yy+=x[i]-'0';
}
if (j>=0)
{
yy+=y[j]-'0';
}
ret=char(yy%10+'0')+ret;
yy/=10;
}
if (yy>0)
{
ret=char(yy+'0')+ret;
}
return ret;
}
string cheng(string x,string y)
{
int lenx=x.length();
int leny=y.length();
string ret="";
vector <int> a(lenx+leny+2,0);
for(int i=lenx-1;i>=0;i--)
{
for(int j=leny-1;j>=0;j--)
{
a[lenx-i+leny-j-1]+=(x[i]-'0')*(y[j]-'0');
}
}
for(int i=1;i<=lenx+leny;i++)
{
a[i+1]+=a[i]/10;
ret=char(a[i]%10+'0')+ret;
}
while(ret[0]=='0'&&ret.length()>1)
{
ret.erase(0,1);
}
return ret;
}
string jian(string x,string y)
{
int lenx=x.length();
int leny=y.length();
int yy=0;
string ret="";
for(int i=lenx-1,j=leny-1;i>=0;i--,j--)
{
yy=x[i]-'0';
if (j>=0)
{
yy-=y[j]-'0';
}
if (yy<0)
{
yy+=10;
x[i-1]--;
}
ret=char(yy+'0')+ret;
}
while(ret[0]=='0'&&ret.length()>1)
{
ret.erase(0,1);
}
return ret;
}
string chu(string x,int y)
{
int yy=0;
string ret="";
for(int i=0;i<x.length();i++)
{
yy=yy*10+x[i]-'0';
ret+=char(yy/y+'0');
yy%=y;
}
while(ret[0]=='0'&&ret.length()>1)
{
ret.erase(0,1);
}
return ret;
}
int main()
{
cin>>n>>m;
if (bj(n,m)==true)
{
swap(n,m);
}
string t1=cheng(m,n);
t1=cheng(t1,jian(t1,jian(jia(m,n),"1")));
string t2=cheng("4",n);
t2=cheng(t2,jian(n,"1"));
t2=cheng(t2,jian(n,"2"));
t2=chu(t2,3);
string t3=jia(jian(m,n),"1");
t3=cheng(t3,"2");
t3=cheng(t3,n);
t3=cheng(t3,jian(n,"1"));
string ans=jian(jian(t1,t2),t3);
cout<<ans;
return 0;
}