Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)
题目链接:
https://codeforces.com/contest/166/problem/E
题目:

题意:
给你一个三菱锥,初始时你在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计数:
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-;
const int mod = ;
const int maxn = 1e7 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n;
LL dp[maxn]; int qpow(int x, int n) {
int res = ;
while(n) {
if(n & ) res = 1LL * res * x % mod;
x = 1LL * x * x % mod;
n >>= ;
}
return res;
} int main(){
int inv = qpow(, mod - );
LL cnt = ;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
cnt = cnt * % mod;
if(i & ) dp[i] = (cnt - + mod) % mod * inv % mod;
else dp[i] = (cnt + ) % mod * inv % mod;
}
printf("%lld\n", dp[n]);
return ;
}
dp计数代码:
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("D://code//in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-;
const int mod = ;
const int maxn = 1e7 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n;
int dp[maxn][]; int main(){
scanf("%d", &n);
dp[][] = ;
for(int i = ; i <= n; i++) {
for(int j = ; j < ; j++) {
for(int k = ; k < ; k++) {
if(j == k) continue;
dp[i][j] = (1LL * dp[i][j] + dp[i-][k]) % mod;
}
}
}
printf("%d\n", dp[n][]);
return ;
}
BM代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
ll powmod(ll a,ll b) {
ll res=;
a%=mod;
assert(b>=);
for(; b; b>>=) {
if(b&)res=res*a%mod;
a=a*a%mod;
}
return res;
}
// head
int _,n;
namespace linear_seq {
const int N=;
ll res[N],base[N],_c[N],_md[N];
vector<int> Md;
void mul(ll *a,ll *b,int k) {
rep(i,,k+k) _c[i]=;
rep(i,,k) if (a[i]) rep(j,,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
for (int i=k+k-; i>=k; i--) if (_c[i])
rep(j,,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
rep(i,,k) a[i]=_c[i];
}
int solve(ll n,VI a,VI b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
// printf("%d\n",SZ(b));
ll ans=,pnt=;
int k=SZ(a);
assert(SZ(a)==SZ(b));
rep(i,,k) _md[k--i]=-a[i];
_md[k]=;
Md.clear();
rep(i,,k) if (_md[i]!=) Md.push_back(i);
rep(i,,k) res[i]=base[i]=;
res[]=;
while ((1ll<<pnt)<=n) pnt++;
for (int p=pnt; p>=; p--) {
mul(res,res,k);
if ((n>>p)&) {
for (int i=k-; i>=; i--) res[i+]=res[i];
res[]=;
rep(j,,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
}
}
rep(i,,k) ans=(ans+res[i]*b[i])%mod;
if (ans<) ans+=mod;
return ans;
}
VI BM(VI s) {
VI C(,),B(,);
int L=,m=,b=;
rep(n,,SZ(s)) {
ll d=;
rep(i,,L+) d=(d+(ll)C[i]*s[n-i])%mod;
if (d==) ++m;
else if (*L<=n) {
VI T=C;
ll c=mod-d*powmod(b,mod-)%mod;
while (SZ(C)<SZ(B)+m) C.pb();
rep(i,,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
L=n+-L,B=T,b=d,m=;
} else {
ll c=mod-d*powmod(b,mod-)%mod;
while (SZ(C)<SZ(B)+m) C.pb();
rep(i,,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
++m;
}
}
return C;
}
int gao(VI a,ll n) {
VI c=BM(a);
c.erase(c.begin());
rep(i,,SZ(c)) c[i]=(mod-c[i])%mod;
return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
}
};
int main() {
scanf("%d",&n);
printf("%d\n",linear_seq::gao(VI{,,,,,,,,,},n-));
}
三份代码跑的时间如下(忽略MLE那发,从上到下分别为BM,DP,公式):

Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)的更多相关文章
- Codeforces Round #265 (Div. 2) C 暴力+ 找规律+ 贪心
C. No to Palindromes! time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #113 (Div. 2)
Codeforces Round #113 (Div. 2) B. Polygons 题意 给一个\(N(N \le 10^5)\)个点的凸包 \(M(M \le 2 \cdot 10^4)\)次询问 ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces 193E - Fibonacci Number(打表找规律+乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 蠢蠢的我竟然第一眼想套通项公式?然鹅显然 \(5\) 在 \(\bmod 10^{13}\) 意义下并没有二次剩余--我真是活回去了... ...
- Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序
B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- Codeforces 998D. Roman Digits 【打表找规律】
<题目链接> 题目大意: 现在有无限个 1,5,10,50这四个数字,从中恰好挑选n个数字,问你这些数字的和总共有多少种不同的情况. 解题分析: 由于此题 n 的范围特别大,达到了1e9, ...
- Codeforces Round #113 (Div. 2) Tetrahedron(滚动DP)
Tetrahedron time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #113 (Div. 2) B. Polygons Andrew求凸包
B. Polygons time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)
Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...
随机推荐
- 正则化项L1和L2
本文从以下六个方面,详细阐述正则化L1和L2: 一. 正则化概述 二. 稀疏模型与特征选择 三. 正则化直观理解 四. 正则化参数选择 五. L1和L2正则化区别 六. 正则化问题讨论 一. 正则化概 ...
- [C/C++] 输入函数getline(cin,str) 与cin.getline(str,int)区别
cin.getline()函数是处理数组字符串的,其原型为cin.getline(char * , int),第一个参数为一个char指针,第二个参数为数组字符串长度. getline(cin,str ...
- Windows系统下搭建Appium自动化测试框架
简介 一种开源的测试框架(http://appium.io/) 能够用来测试原生Android/iOS应用.混合应用以及webapp 通过webdriver协议来操作应用,其核心是一个web服务器,接 ...
- SPOJ_LCS2
和上个题目差不多,这次是找若干个串的LCS,若干<=10 . 做法上面也是类似的. 首先以第一个建立SAM,然后后面的串都在上面更新.每个串的更新是独立进行的,互不影响.对于同一状态,首先在同一 ...
- P3758 [TJOI2017]可乐
题目描述 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且放在了加里敦星球的1号城市上.这个可乐机器人有三种行为: 停在原地,去下一个相邻的城市,自爆.它每一秒都会随机 ...
- 批量更新 A表的PK_ID字段
UPDATE ASET PK_ID=(SELECT ID FROM B WHERE A.TAB_NAME=B.TAB_NAME AND B.IS_KEY='1' ) AB表 以TAB_NAME 做 ...
- 【比赛】NOIP2017 奶酪
开始看到题以为是计算几何,后面发现不是,然后秒掉了. 可能写SPFA写多了,别人都是并查集做的,我用的是SPFA. 不过无所谓,我们把题目中的下底面和上表面看成两个点,那么就是求这两个点的连通性,如果 ...
- BZOJ 3881: [Coci2015]Divljak
3881: [Coci2015]Divljak Time Limit: 20 Sec Memory Limit: 768 MBSubmit: 553 Solved: 176[Submit][Sta ...
- 【XSY2307】树的难题
Description Solution 看到这种路径统计问题,一般就想到要用点分治去做. 对于每个重心\(u\),统计经过\(u\)的合法的路径之中的最大值. 第一类路径是从\(u\)出发的,直接逐 ...
- 【bzoj3122】 Sdoi2013—随机数生成器
http://www.lydsy.com/JudgeOnline/problem.php?id=3122 (题目链接) 题意 对于一个数列${X_i}$,其递推式为:${X_{i+1}=(a*X_i+ ...