#include<bits/stdc++.h>
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 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("SIZE %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 a[][];
long long det(int n) {
long long ans = ;
for (int i = ; i < n; i++) {
for (int j = i + ; j < n; j++) {
while (a[j][i] != ) {
int u = a[i][i] / a[j][i];
for (int k = ; k < n; k++) {
int t = (a[i][k] - (long long)a[j][k] * u % mod + mod) % mod;
a[i][k] = a[j][k];
a[j][k] = t;
}
ans = -ans;
}
}
ans = ans * a[i][i] % mod;
}
if (ans < ) {
ans += mod;
}
return ans;
} long long work(int k, int n) {
// assert(n > 2 * k + 1);
memset(a, , sizeof a);
for (int i = ; i < n; i++) {
a[i][i] = k;
for (int j = ; j <= k; j++) {
a[i][(i + j) % n] = -;
}
}
/*
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d%c", a[i][j], j == n - 1 ? '\n' : ' ');
}
}
*/
int t = ;
for (int i = ; i < k; i++) {
t = t * i % mod;
}
return (long long)det(n - ) * powmod(t, n) % mod;
} void maketable() {
printf("{\n");
for (int k = ; k <= ; k++) {
if (k > ) {
printf(",\n");
}
printf("{\n");
for (int i = * k + ; i <= ; i++) {
if (i > * k + ) {
printf(",");
}
printf("%lld", work(k, i));
}
printf("}\n");
}
printf("}\n");
}
int main() {
int k;
long long n;
// maketable();
cin >> k >> n;
// k = 5;
// printf("%d\n", work(k, n));
vector<int> a;
for (int i = * k + ; i <= ( << k) + * k + ; i++) {
// printf("%d %d\n", i, work(k, i));
a.push_back(work(k, i));
}
// cout << linear_seq::gao(b[k], n - (2 * k + 1)) << endl;
cout << linear_seq::gao(a, n - ( * k + )) << endl;
return ;
}

//BM

牛客第九场欧拉回路

杜教BM模板的更多相关文章

  1. ZZNU 2182 矩阵dp (矩阵快速幂+递推式 || 杜教BM)

    题目链接:http://47.93.249.116/problem.php?id=2182 题目描述 河神喜欢吃零食,有三种最喜欢的零食,鱼干,猪肉脯,巧克力.他每小时会选择一种吃一包. 不幸的是,医 ...

  2. BZOJ3944: Sum(杜教筛模板)

    BZOJ3944: Sum(杜教筛模板) 题面描述 传送门 题目分析 求\(\sum_{i=1}^{n}\mu(i)\)和\(\sum_{i=1}^{n}\varphi(i)\) 数据范围线性不可做. ...

  3. 牛客多校第九场 A The power of Fibonacci 杜教bm解线性递推

    题意:计算斐波那契数列前n项和的m次方模1e9 题解: $F[i] – F[i-1] – F[i-2] = 0$ $F[i]^2 – 2 F[i-1]^2 – 2 F[i-2]^2 + F[i-3] ...

  4. [bzoj3944] sum [杜教筛模板]

    题面: 传送门 就是让你求$ \varphi\left(i\right) $以及$ \mu\left(i\right) $的前缀和 思路: 就是杜教筛的模板 我们把套路公式拿出来: $ g\left( ...

  5. 【Bzoj3944】杜教筛模板(狄利克雷卷积搞杜教筛)

    题目链接 哇杜教筛超炫的 有没有见过$O(n^\frac{2}{3})$求欧拉函数前缀和的算法?没有吧?蛤蛤蛤 首先我们来看狄利克雷卷积是什么 首先我们把定义域是整数,陪域是复数的函数叫做数论函数. ...

  6. 杜教BM【转载】

    https://blog.csdn.net/qq_36876305/article/details/80275708 #include <bits/stdc++.h> using name ...

  7. 杜教BM

    #include <algorithm> #include <iterator> #include <iostream> #include <cstring& ...

  8. 杜教BM递推板子

    Berlekamp-Massey 算法用于求解常系数线性递推式 #include<bits/stdc++.h> typedef std::vector<int> VI; typ ...

  9. 黑科技之杜教bm

    这个板子能够解决任何线性递推式,只要你确定某个数列的某项只与前几项线性相关,那么把它前若干项丢进去,这个板子就能给你返回你要求的某项的值. 原理???(待补充) #include<bits/st ...

随机推荐

  1. Implementing a Dynamic Vector (Array) in C(使用c实现动态数组Vector)

    An array (vector) is a common-place data type, used to hold and describe a collection of elements. T ...

  2. 关于SVN的405错误Server sent unexpected return value (405 Method Not Allowed)的解决办法

    一大早上捣鼓项目提交的时候出现这个错误: svn:server sent unexpected return value 405 method not allowed 百度了很多解决办法都没有解决,看 ...

  3. configmap使用-完整的configmap文档

    转发 https://www.jianshu.com/p/cf3e2218f283 转发 https://www.kubernetes.org.cn/3138.html 注意:configmap不用也 ...

  4. MathType 6.0中MT Extra(TrueType)问题

    问题 MathType 6.0中MT Extra(TrueType)字体问题在打开MathType6.0时,有时会提示MathType需要安装一个较新版本的MT Extra(TrueType)字体,这 ...

  5. 【OpenGL】初识OpenGL4.0

    目录(?)[-] 什么是GLSL GLEW 安装GLEW 使用GLEW 其他库 使用GLM库进行数学运算 安装GLM 使用GLM 使用GLM作为OpenGL的输入 使用GLFW进行窗口管理 这篇文章主 ...

  6. 【DSP开发】回马枪要你命 德州仪器发布最强ARM芯片Keystone II

    之前许多传闻称德州仪器将会彻底放弃OMAP系列ARM处理器,从此离开手持设备的江湖.如果你信以为真,那可就太小看德州仪器这个老狐狸了--要知道德州仪器诞生的比Intel都还早几年.三小时前,德州仪器宣 ...

  7. 封装自己的jquery插件

    自己尝试封装的一个在工作当中使用的多级弹窗插件: ;(function ($, window, document) { //用一个自调用匿名函数把插架代码包裹起来,防止代码污染 $.fn.multi ...

  8. DQN算法原理详解

    一. 概述 强化学习算法可以分为三大类:value based, policy based 和 actor critic. 常见的是以DQN为代表的value based算法,这种算法中只有一个值函数 ...

  9. Centos8 重启网卡方法

    问题情况: 1.虚机centos8 修改为静态ip后,由于网卡网段变更,无法上网 2.最小化安装,没有ifconfig 3.firewalld,selinux关闭 4.ping 不通物理机 根本原因: ...

  10. linux yum安装过程终止方法

    //中断当前的安装显示 ctrl+z //查找当前yum相关的进程 ps -ef | grep yum //杀掉进程 进程号(pid)