#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. Delphi GetDir 获取指定驱动器的当前路径名

    //获取指定驱动器的当前路径名 GetDirvardir: string;b: Byte;beginb := 0;GetDir(b,dir);ShowMessage(dir); // //第一个参数: ...

  2. 083. Remove Duplicates from Sorted List

    题目链接:https://leetcode.com/problems/rotate-list/description/ Given a sorted linked list, delete all d ...

  3. Python - 数据结构与算法(Data Structure and Algorithms)

    入门 The Algorithms Python https://github.com/TheAlgorithms/Python 从基本原理到代码实现的Python算法入门,简洁地展示问题怎样解决,因 ...

  4. Typora快捷键记录

    目前使用的是Typora这款Markdown软件,记录一下快捷键用法: 1.无序列表,黑色实心圆点或白色空心圆点 首先去除缩进,使用“Ctrl”+"["键或者“Shift”+&qu ...

  5. word2010目录和正文分开编页码

    最近公司要写文档,老板给了一个范文模板,其中目录和正文的页码就是分开编辑的.上网查了很多资料,基本上都没有附图,通过自己的摸索以后终于实现了.现在我就把实现过程跟截图一并奉上.

  6. Elasticsearch-日期类型

    Elasticsearch-日期类型 date类型用于存储日期和时间.它是这样运作的:通常提供一个表示日期的字符串,例如2019-06-25T22:47.然后,ES解析这个字符串,然后将其作为long ...

  7. NLP自然语言处理中英文分词工具集锦与基本使用介绍

    一.中文分词工具 (1)Jieba (2)snowNLP分词工具 (3)thulac分词工具 (4)pynlpir 分词工具 (5)StanfordCoreNLP分词工具 1.from stanfor ...

  8. myeclipse显示db-brower

    myeclipse显示db-brower 这东西怎么调出来? windows->show view->other->db borwser

  9. MySQL创表--分页--自关联--

    创建表book create table t_book( id int unsigned auto_increment primary key, bookName varchar(255) defau ...

  10. Java重要类之LinkedList

    一.ArrayList与LinkedList 基本概念:List是一个接口,Arraylist和LinkedList是它的两个实现类,只是实现的方式不一样.我在“单链表java实现”一文中已经对单链表 ...