http://codeforces.com/problemset/problem/300/D

题意:每一次操作可以选一个正方形,令边长为n,如果n为奇数那么可以从中间画一个十字,分成4个大小相等的边长为(n-1)/2的正方形。给一个正方形,求操作k次后能得到的不同图案的个数

思路:令f(s,k)表示边长为s的正方形操作k次后的答案总数,则f(s,k)=∑f(s/2,k1)*f(s/2,k2)*f(s/2,k3)*f(s/2,k4),其中s为奇数,k1+k2+k3+k4=k-1,令g(s,k)=Σf(s,k1)*f(s,k2),其中s为奇数,k1+k2=k。那么有f(s,k)=Σg(s/2,k')*g(s/2,k-1-k'),g(s,k)=Σf(s,k')*f(s,k-k'),其中s为奇数,k'取遍所有可能的值。由于s最多递归logs层,所以用一个不超过logs的数和k表示状态即可,然后递推计算,复杂度O(k2logs)。

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; //#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=;i<a.size();i++)a[i]=b[i];} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-8; /* -------------------------------------------------------------------------------- */ template<int mod>
struct ModInt {
const static int MD = mod;
int x;
ModInt(ll x = ): x(x % MD) {}
int get() { return x; } ModInt operator + (const ModInt &that) const { int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); }
ModInt operator - (const ModInt &that) const { int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); }
ModInt operator * (const ModInt &that) const { return ModInt((long long)x * that.x % MD); }
ModInt operator / (const ModInt &that) const { return *this * that.inverse(); } ModInt operator += (const ModInt &that) { x += that.x; if (x >= MD) x -= MD; }
ModInt operator -= (const ModInt &that) { x -= that.x; if (x < ) x += MD; }
ModInt operator *= (const ModInt &that) { x = (long long)x * that.x % MD; }
ModInt operator /= (const ModInt &that) { *this = *this / that; } ModInt inverse() const {
int a = x, b = MD, u = , v = ;
while(b) {
int t = a / b;
a -= t * b; std::swap(a, b);
u -= t * v; std::swap(u, v);
}
if(u < ) u += MD;
return u;
} };
typedef ModInt<> mint; mint dp[][], f[][]; void pre_init() {
for (int i = ; i <= ; i ++) dp[i][] = f[i][] = ;
for (int i = ; i <= ; i ++) {
dp[i][] = ;
f[i][] = ;
for (int j = ; j <= ; j ++) {
for (int k = ; k <= j; k ++) {
dp[i][j] += f[i - ][k] * f[i - ][j - k - ];
}
for (int k = ; k <= j; k ++) {
f[i][j] += dp[i][k] * dp[i][j - k];
}
}
}
} int work(int n, int k) {
if (k == ) return ;
int p = ;
while (n & ) {
p ++;
n = (n - ) / ;
}
if (n) p ++;
return dp[p][k].get();
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int n, T, k;
pre_init();
while (cin >> T) {
while (T --) {
scanf("%d%d", &n, &k);
printf("%d\n", work(n, k));
}
}
return ;
}

[CodeForces 300D Painting Square]DP的更多相关文章

  1. CF300D Painting Square

    Painting Square https://codeforces.com/problemset/problem/300/D 给了一个理解起来较复杂但是本质上很简单的分形. 题解 很显然,只有边长为 ...

  2. Codeforces 828B Black Square(简单题)

    Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n × m. ...

  3. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  4. Codeforces - 1198D - Rectangle Painting 1 - dp

    https://codeforces.com/contest/1198/problem/D 原来是dp的思路,而且是每次切成两半向下递归.好像在哪里见过类似的,貌似是紫书的样子. 再想想好像就很显然的 ...

  5. Educational Codeforces Round 67 E.Tree Painting (树形dp)

    题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...

  6. Codeforces 840C 题解(DP+组合数学)

    题面 传送门:http://codeforces.com/problemset/problem/840/C C. On the Bench time limit per test2 seconds m ...

  7. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  8. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  9. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

随机推荐

  1. Project configuration is not up-to-date with pom.xml.错误

    完整错误信息:Description Resource Path Location TypeProject configuration is not up-to-date with pom.xml. ...

  2. xshell下使用vim的编辑一个文件Ctrl+S和Ctrl+Q

    xshell下使用vim的编辑一个文件,保存的时候习惯性的按了Ctrl+S 结构悲剧了.屏幕锁死了.按其他键都没有反应,exc也不行. 经过问度娘才知道. 原来Ctrl+S在Linux里,是锁定屏幕的 ...

  3. DataTable运用

    DataTable dataDis.AsEnumerable().Sum(bu => bu["QtyPlan"].ConvertInt32()); ndata.TDefSty ...

  4. python-Django与Apache整合wsgi模块

    1.安装wsgi模块 yum search mod_wsgi yum install -y mod_wsgi 2.会在httpd下有配置文件 cd /etc/httpd/conf.d/wsgi.con ...

  5. 后缀数组SA

    复杂度:O(nlogn) 注:从0到n-1 const int maxn=1e5; char s[maxn]; int sa[maxn],Rank[maxn],height[maxn],rmq[max ...

  6. Autofac的切面编程实现

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  7. 编写C#程序的IDE

    编写C#程序,在Windows平台下,除了昂贵的Visual Studio.NET这个正宗的工具外,你还了解哪些? 听说有个Eclipse,IBM投钱开发的开源工具,有人也做了个for .NET的pl ...

  8. 数论--HDU 1495 非常可乐

    Description 大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和se ...

  9. 数据库SQL语言从入门到精通--Part 1--SQL语言概述

    数据库从入门到精通合集(超详细,学习数据库必看) 一.SQL概述 关系数据库标准语言SQL(结构化查询语言). 结构化查询语言(Structured Query Language)简称SQL,是一种特 ...

  10. Codeforces Round 623(Div. 2,based on VK Cup 2019-2020 - Elimination Round,Engine)D. Recommendations

    VK news recommendation system daily selects interesting publications of one of n disjoint categories ...