首页 > 其他分享 >C. How Does the Rook Move?

C. How Does the Rook Move?

时间:2024-04-22 20:33:37浏览次数:10  
标签:Rook int res ll cin How Does include dp

https://codeforces.com/contest/1957/problem/C
题面:

题解:

补充说明:
把n阶棋盘想成n-1阶和外面套了一圈的如上图
则可以分成下面两种情况:
1.

下在(1,1)处,那么为dp[n-1]
2.

从(1,2)...(1,n-1)|(2,1)...(n-1,1)共有n-1种显然为2(n-1)dp[n-2]
那么递推:
dp[n]=dp[n-1]+2*(n-1)*dp[n-2]
代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#include<set>

typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
ll dp[300010];
ll dpp(ll n)
{
	if (dp[n])return dp[n];
	ll res = 0;
	res = dpp(n - 1) + (n - 1 ) * 2 * dpp(n - 2);
	res %= mod;
	return dp[n] = res;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t; cin >> t;
	dp[1] = 1;
	dp[2] = 3;
	while (t--)
	{
		ll n; cin >> n;
		int k; cin >> k;
		for (int i = 0; i < k; i++)
		{
			int x, y; cin >> x >> y;
			if (x == y)n--;
			else n -= 2;
		}
		if (n == 0)cout << 1 << endl;
		else cout << dpp(n) << endl;
	}
	return 0;
}

标签:Rook,int,res,ll,cin,How,Does,include,dp
From: https://www.cnblogs.com/zzzsacmblog/p/18151461

相关文章

  • CTFshow-Web入门模块-命令执行
    CTFshow-Web入门模块-命令执行by故人叹、web29考察点:php命令执行、正则匹配绕过题目源码:error_reporting(0);if(isset($_GET['c'])){$c=$_GET['c'];if(!preg_match("/flag/i",$c)){eval($c);}}else{highlight_file(__FILE__);......
  • C. How Does the Rook Move?
    原题链接题解1.没有棋下也算一种情况!!!!!!!!!2.前k次放置,如果放在对角线上,会把原先n阶矩阵分解成n-1阶矩阵,如果没放在对角线上,会分割成n-2阶矩阵3.不管如何放置,到最后第一列总有且仅有一颗棋子,第一列的第一行只能放白棋,第2到第n行可以放白棋或黑棋,所以第一列的放置有\(2n-1\)种情况......
  • CF1097F Alex and a TV Show 题解
    题目链接点击打开链接题目解法很牛的套路啊!看到集合并,且只要求奇偶性的问题,第一个想到\(bitset\)\(1,2,4\)操作都是好维护的,关键是第\(3\)个操作看到$\gcd$,首先想到莫反令\(c_{x,i}\)为集合\(x\)中数\(i\)的出现次数则\(c_{x,i}=\sum\limits_{i|j}\sum\limit......
  • cv2.imshow显示图片不全
    我们在屏幕上显示图片会用这个命令:cv2.imshow('img',pic),假如图片分辨率比较大,那么会出现显示不全的情况,例如:解决办法:在cv2.imshow前面加上cv2.namedWindow代码注意:两行代码中的窗口名必须保持一致不然无效,我这里都是'img'cv2.namedWindow('img',cv2.WINDOW_NORMAL)......
  • wpf datagrid,menuitem, style, export ,show in a another window,mvvm
    //xaml<Windowx:Class="WpfApp58.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • [NeuralPS2023]How Re-sampling Helps for Long-Tail Learning
    这篇文章作者写得非常详细,读起来非常舒适。Contribution:在long-taileddata中,re-sampling不一定有效。re-sampling的失败可能是对于不相关的context过拟合导致的,作者设计了实验论证了这一假说。在single-stage的框架下,作者提出了上下文转换增强(contextualtransformationau......
  • CTFshow-Web入门模块-爆破
    CTFshow-Web入门模块-爆破by故人叹、web21考察点:burpsuite使用、自定义迭代器使用题目提供了字典,要求登录并提示了爆破,使用burpsuite抓包并操作:(1)用户名一般为admin,密码随意输,抓包并发送给爆破模块。可以看到Basic后有一段base64编码,解码得到用户名:密码的格式,证明我们之......
  • CTFshow-Web入门模块-信息搜集writeip
    CTFshow-Web入门模块-信息搜集writeipby故人叹、信息搜集web1考察点:查看源码题目提示:”开发注释未及时删除“,页面源代码中可能有东西,查看页面源代码发现flag。总结查看源码的几种方式:F12打开源代码快捷键Ctrl+u在url前面加上view-source:浏览器右上角选项中的Web开......
  • CTFshow Reverse 逆向4 学习记录
     题目 分析过程 是一个无壳,64位的文件丢到IDA里面,找到main函数1int__cdecl__noreturnmain(intargc,constchar**argv,constchar**envp)2{3__int64v3;//rdx4char*v4;//[rsp+20h][rbp-18h]56qword_140004618=(__int64)malloc(1......
  • 52 Things: Number 33: How does the Bellcore attack work against RSA with CRT?
    52Things:Number33:HowdoestheBellcoreattackworkagainstRSAwithCRT?52件事:第33件:Bellcore攻击如何使用CRT对抗RSA? Thisisthelatestinaseriesofblogpoststoaddressthelistof'52ThingsEveryPhDStudentShouldKnowToDoCryptography':......