CF-295D-Greg and Caves(dp+思维)

Posted --hpy-7m

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF-295D-Greg and Caves(dp+思维)相关的知识,希望对你有一定的参考价值。

题意:http://codeforces.com/problemset/problem/295/D

技术图片

思路:

可以把图形看成上下两个金字塔(极端情况下长方形),dp出以i的长为底,以j为高的三角形数(长方形),再预处理前缀(以i的长为底,以<=j为高),然后就枚举中心行,上下金字塔数相乘,注意两个图形的底边相同导致重复的可能性

  1 #define ios ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:Users13606DesktopInput.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr strcat
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 clock_t __START,__END;
 27 double __TOTALTIME;
 28 void _MS(){__START=clock();}
 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 30 //***********************
 31 #define rint register int
 32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 34 #define mem(a,b) memset(a,b,sizeof(a))
 35 #define pr printf
 36 #define sc scanf
 37 #define ls rt<<1
 38 #define rs rt<<1|1
 39 typedef pair<int,int> PII;
 40 typedef vector<int> VI;
 41 typedef unsigned long long ull;
 42 typedef long long ll;
 43 typedef double db;
 44 const db E=2.718281828;
 45 const db PI=acos(-1.0);
 46 const ll INF=(1LL<<60);
 47 const int inf=(1<<30);
 48 const db ESP=1e-9;
 49 const int mod=(int)1e9+7;
 50 const int N=(int)2e3+10;
 51 
 52 ll sq[N][N];
 53 ll sum[N];
 54 
 55 int main()
 56 {
 57     int n,m;
 58     sc("%d%d",&n,&m);
 59     if(m<2)
 60     {
 61         pr("0
");
 62         return 0;
 63     }
 64     for(int i=2;i<=m;++i)
 65         sq[i][1]=1;
 66     for(int i=2;i<=n;++i)
 67     {
 68         sq[2][i]=1;
 69         sum[2]=1;
 70         for(int j=3;j<=m;++j)
 71             sum[j]=sq[j][i-1]+sum[j-1],sum[j]%=mod;
 72         ll temp=1;
 73         for(int j=3;j<=m;++j)
 74             temp+=sum[j],temp%=mod,sq[j][i]=temp;
 75     }
 76 //    for(int i=1;i<=n;++i)
 77 //        for(int j=2;j<=m;++j)
 78 //        {
 79 //            pr("%d %d: %lld
",j,i,sq[j][i]);
 80 //        }
 81     for(int j=2;j<=m;++j)
 82         for(int i=2;i<=n;++i)
 83         {
 84             sq[j][i]+=sq[j][i-1];
 85             sq[j][i]%=mod;
 86         }
 87     ll ans=0;
 88 //    for(int i=2;i<=m;++i)sq[i][0]=1;
 89     for(int i=1;i<=n;++i)
 90     {
 91         for(int j=2;j<=m;++j)sum[j]=sq[j][n-i];
 92         for(int j=3;j<=m;++j)sum[j]+=sum[j-1],sum[j]%=mod;
 93         ans+=sq[2][i]*(m-2+1)%mod;
 94         ans%=mod;
 95         ll temp=0;
 96         for(int j=3;j<=m;++j)
 97             ans+=sq[j][i]*(m-j+1)%mod,ans%=mod;
 98         for(int j=3;j<=m;++j)
 99         {
100             temp+=sum[j-1];
101             temp%=mod;
102             temp+=sq[j-1][n-i];
103             temp%=mod;
104             ans+=sq[j][i]*temp%mod*(m-j+1)%mod;
105             ans%=mod;
106         //    temp+=sum[j];
107         }
108     }
109     pr("%lld
",ans);
110     return 0;
111 }
112 
113 /**************************************************************************************/

 

以上是关于CF-295D-Greg and Caves(dp+思维)的主要内容,如果未能解决你的问题,请参考以下文章

URAL 题目1553. Caves and Tunnels(Link Cut Tree 改动点权,求两点之间最大)

关于安排

Fxx and game hdu 5945 斜率优化dp

CodeForces 416 B Appleman and Tree DP

D. Roman and Numbers(状压dp)

codeforces 416B. Appleman and Tree 树形dp