博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)
阅读量:4597 次
发布时间:2019-06-09

本文共 3863 字,大约阅读时间需要 12 分钟。

题目链接:

  

题目:

题意:

  给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点的方案数。

思路:

  打表找规律得到的序列是0,3,6,21,60,183,546,1641,4920,14763,通过肉眼看或者oeis可以得到规律为

  dp计数:dp[i][j]表示在第i步时站在位置j的方案数,j的取值为[0,3],分别表示D,A,B,C点,转移方程肯定是从其他三个点转移。

代码实现如下:

 非dp计数:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 using namespace std;17 18 typedef long long LL;19 typedef pair
pLL;20 typedef pair
pLi;21 typedef pair
pil;;22 typedef pair
pii;23 typedef unsigned long long uLL;24 25 #define lson rt<<126 #define rson rt<<1|127 #define lowbit(x) x&(-x)28 #define name2str(name) (#name)29 #define bug printf("*********\n")30 #define debug(x) cout<<#x"=["<
<<"]" <
>= 1;50 }51 return res;52 }53 54 int main(){55 int inv = qpow(4, mod - 2);56 LL cnt = 1;57 scanf("%d", &n);58 for(int i = 1; i <= n; i++) {59 cnt = cnt * 3 % mod;60 if(i & 1) dp[i] = (cnt - 3 + mod) % mod * inv % mod;61 else dp[i] = (cnt + 3) % mod * inv % mod;62 }63 printf("%lld\n", dp[n]);64 return 0;65 }

dp计数代码:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 using namespace std;17 18 typedef long long LL;19 typedef pair
pLL;20 typedef pair
pLi;21 typedef pair
pil;;22 typedef pair
pii;23 typedef unsigned long long uLL;24 25 #define lson rt<<126 #define rson rt<<1|127 #define lowbit(x) x&(-x)28 #define name2str(name) (#name)29 #define bug printf("*********\n")30 #define debug(x) cout<<#x"=["<
<<"]" <

BM代码:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 using namespace std; 11 #define rep(i,a,n) for (int i=a;i
=a;i--) 13 #define pb push_back 14 #define mp make_pair 15 #define all(x) (x).begin(),(x).end() 16 #define fi first 17 #define se second 18 #define SZ(x) ((int)(x).size()) 19 typedef vector
VI; 20 typedef long long ll; 21 typedef pair
PII; 22 const ll mod=1000000007; 23 ll powmod(ll a,ll b) { 24 ll res=1; 25 a%=mod; 26 assert(b>=0); 27 for(; b; b>>=1) { 28 if(b&1)res=res*a%mod; 29 a=a*a%mod; 30 } 31 return res; 32 } 33 // head 34 int _,n; 35 namespace linear_seq { 36 const int N=100100; 37 ll res[N],base[N],_c[N],_md[N]; 38 vector
Md; 39 void mul(ll *a,ll *b,int k) { 40 rep(i,0,k+k) _c[i]=0; 41 rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod; 42 for (int i=k+k-1; i>=k; i--) if (_c[i]) 43 rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod; 44 rep(i,0,k) a[i]=_c[i]; 45 } 46 int solve(ll n,VI a,VI b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+... 47 // printf("%d\n",SZ(b)); 48 ll ans=0,pnt=0; 49 int k=SZ(a); 50 assert(SZ(a)==SZ(b)); 51 rep(i,0,k) _md[k-1-i]=-a[i]; 52 _md[k]=1; 53 Md.clear(); 54 rep(i,0,k) if (_md[i]!=0) Md.push_back(i); 55 rep(i,0,k) res[i]=base[i]=0; 56 res[0]=1; 57 while ((1ll<
<=n) pnt++; 58 for (int p=pnt; p>=0; p--) { 59 mul(res,res,k); 60 if ((n>>p)&1) { 61 for (int i=k-1; i>=0; i--) res[i+1]=res[i]; 62 res[0]=0; 63 rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod; 64 } 65 } 66 rep(i,0,k) ans=(ans+res[i]*b[i])%mod; 67 if (ans<0) ans+=mod; 68 return ans; 69 } 70 VI BM(VI s) { 71 VI C(1,1),B(1,1); 72 int L=0,m=1,b=1; 73 rep(n,0,SZ(s)) { 74 ll d=0; 75 rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod; 76 if (d==0) ++m; 77 else if (2*L<=n) { 78 VI T=C; 79 ll c=mod-d*powmod(b,mod-2)%mod; 80 while (SZ(C)

三份代码跑的时间如下(忽略MLE那发,从上到下分别为BM,DP,公式):

 

转载于:https://www.cnblogs.com/Dillonh/p/10447282.html

你可能感兴趣的文章
Mask R-CNN详解和安装
查看>>
poj2017
查看>>
【程序员人生】优秀程序员的法则
查看>>
cocos2d下,优秀骨骼spine的换装思路
查看>>
Windows 10 MBR转GPT
查看>>
iuplua test failure
查看>>
6 tr
查看>>
同开三本DJANGO,需要提升一下本职工作的能力啦
查看>>
这样就算会了PHP么?-2
查看>>
线段树 (区间查询最大 区间求和 区间加)带lazy
查看>>
三十而立,从零开始学ios开发(十二):Table Views(上)
查看>>
MySQL中的decimal
查看>>
gitlab+jenkins持续集成(一)
查看>>
4.signed/unsigned char
查看>>
iOS,UIImage有个contentmodel属性
查看>>
Debian 7 amd64 + fbterm + ucimf
查看>>
数据结构之【排序】复习题
查看>>
spring boot 首次请求Controller慢
查看>>
事件绑定
查看>>
grep命令详解
查看>>