奇怪的道路
题目描述:(暂不提供)
由于这道题数据范围很大,所以显然不可以直接搞。
我们设 为 阶城市位置为 的编号。
观察前两个图,就可以发现:
当? = 0时,其结果为1,否则,我们通过对图形的观察可知:
当(?, ?)在左上部分时,结果即为:
0 ×
+
(? − 1, ?, ?)。
当(?, ?)在右上部分时,结果即为:
1 ×
+
(? − 1, ?, ? − 2? − 1)。
当(?, ?)在右下部分时,结果即为:
2 ×
+
(? − 1, ? − 2? − 1, ? − 2? − 1)。
当(?, ?)在左下部分时,结果即为:
3 ×
+
(? − 1, 2? − 1 + 1 −?, 2? + 1 −?)。
然后既可以用搜索轻松解决。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll n,m,x1,x2,y1,y2;
ll dfs(ll n,ll x,ll y)
{
if(n == 0) return 1;
ll now = 1 << (n - 1),tot = now * now;
if(x <= now && y <= now) return dfs(n - 1,y,x);
else if(x <= now && y > now) return tot + dfs(n - 1,x,y - now);
else if(x > now && y > now) return 2 * tot + dfs(n - 1,x - now,y - now);
else return tot * 3 + dfs(n - 1,now + 1 - y,2 * now + 1 - x);
}
int main()
{
freopen("road.in","r",stdin);
freopen("road.out","w",stdout);
scanf("%lld%lld",&n,&m); ++m;
for(;--m;)
{
scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
printf("%lld\n",abs(dfs(n,x1,y1) - dfs(n,x2,y2)));
}
fclose(stdin); fclose(stdout);
return 0;
}
也就这样。但貌似 没有推出来。