基本思想
example
- 归并排序
https://www.cnblogs.com/orion7/p/8242774.html - 快速幂
int ksm(int x,int y,int p)
{
int ret=1;
while(y)
{
if(y&1) ret=(ret*x)%p;
x=x*x%p;y=y>>1;
}
return ret;
}
例题
- luogu P1228 https://www.luogu.org/problemnew/show/P1228
https://blog.csdn.net/SSL_ZYC/article/details/82712832
solution
- 我们发现在一个2*2的块内并且其中有公主或者已经有一块填好,这种情况很简单就是1,2,3,4四种方法
- 我们考虑这张图,取中间点分成四块,非公主的三块补一块,然后这四大块就是一种情况了,最后递归解决
- 为什么是分治,我们发现2^k这个条件其实很明显
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
inline int read(){
char ch=' ';int f=1;int x=0;
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
void dfs(int x1,int y1,int x2,int y2,int X,int Y)
{
if(x2-x1==1)
{
if(X==x1&&Y==y1)cout<<x2<<' '<<y2<<" 1"<<endl;
else if(X==x1&&Y==y2)cout<<x2<<' '<<y1<<" 2"<<endl;
else if(X==x2&&Y==y1)cout<<x1<<' '<<y2<<" 3"<<endl;
else cout<<x1<<' '<<y1<<" 4"<<endl;
return ;
}
int x=(x1+x2)>>1;
int y=(y1+y2)>>1;
if (X<=x&&Y<=y)
{
dfs(x1,y1,x,y,X,Y);
printf("%d %d 1\n",x+1,y+1);
dfs(x+1,y1,x2,y,x+1,y);
dfs(x+1,y+1,x2,y2,x+1,y+1);
dfs(x1,y+1,x,y2,x,y+1);
}
if (X<=x&&Y>y)
{
dfs(x1,y+1,x,y2,X,Y);
printf("%d %d 2\n",x+1,y);
dfs(x1,y1,x,y,x,y);
dfs(x+1,y1,x2,y,x+1,y);
dfs(x+1,y+1,x2,y2,x+1,y+1);
}
if (X>x&&Y<=y)
{
dfs(x+1,y1,x2,y,X,Y);
printf("%d %d 3\n",x,y+1);
dfs(x+1,y+1,x2,y2,x+1,y+1);
dfs(x1,y1,x,y,x,y);
dfs(x1,y+1,x,y2,x,y+1);
}
if (X>x&&Y>y)
{
dfs(x+1,y+1,x2,y2,X,Y);
printf("%d %d 4\n",x,y);
dfs(x1,y1,x,y,x,y);
dfs(x1,y+1,x,y2,x,y+1);
dfs(x+1,y1,x2,y,x+1,y);
}
}
int main()
{
int k;k=read();int X,Y;X=read();Y=read();int n=1;
for(int i=1;i<=k;i++)n=n*2;
dfs(1,1,n,n,X,Y);
return 0;
}