日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[CodeForces 300D Painting Square]DP

發布時間:2025/4/16 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [CodeForces 300D Painting Square]DP 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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=0;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?1:-1; 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?1:-1;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=0;i<b.size();i++)a[i]=b[i];} template<typename T> void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];}const double PI = acos(-1.0); const int INF = 1e9 + 7; const double EPS = 1e-8;/* -------------------------------------------------------------------------------- */template<int mod> struct ModInt {const static int MD = mod;int x;ModInt(ll x = 0): 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 < 0) 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 = 1, v = 0;while(b) {int t = a / b;a -= t * b; std::swap(a, b);u -= t * v; std::swap(u, v);}if(u < 0) u += MD;return u;}}; typedef ModInt<7340033> mint;mint dp[31][1010], f[31][1010];void pre_init() {for (int i = 1; i <= 30; i ++) dp[i][0] = f[i][0] = 1;for (int i = 2; i <= 30; i ++) {dp[i][1] = 1;f[i][1] = 2;for (int j = 2; j <= 1000; j ++) {for (int k = 0; k <= j; k ++) {dp[i][j] += f[i - 1][k] * f[i - 1][j - k - 1];}for (int k = 0; k <= j; k ++) {f[i][j] += dp[i][k] * dp[i][j - k];}}} }int work(int n, int k) {if (k == 0) return 1;int p = 0;while (n & 1) {p ++;n = (n - 1) / 2;}if (n) p ++;return dp[p][k].get(); }int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout); #endif // ONLINE_JUDGEint n, T, k;pre_init();while (cin >> T) {while (T --) {scanf("%d%d", &n, &k);printf("%d\n", work(n, k));}}return 0; }

轉載于:https://www.cnblogs.com/jklongint/p/4726319.html

總結

以上是生活随笔為你收集整理的[CodeForces 300D Painting Square]DP的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。