#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. eclipse下写html

    3.创建静态web工程 打开eclipse,选择file,new project 或者 new other...,选择web项中的static web project ,next. 输入你的项目名,如 ...

  2. 使用robotframework做接口测试4——搞定接口签名及密码加密

    接口签名是我做接口测试撞到的第一块石头,刚到新公司,本来想默默憋出来一个大招,然后碰到了签名接口,直接给跪了.于是只好找开发哥哥帮忙,把签名算法的java文件拿到,一行行对着用python实现了一遍. ...

  3. Autofac实现AOP拦截

    本文主要是详解一下在ASP.NET Core中,采用替换后的Autofac来实现AOP拦截. Aspect Oriented Programming(AOP),面向切面编程,是一个比较热门的话题.AO ...

  4. JavaScript高级编程学习笔记(第三章之一)

    继续记笔记,JavaScript越来越有意思了. 继续... 第三章:JavaScript基础 ECMAScript语法在很大程度上借鉴了C和其它类似于C的语言,比如Java和Perl. 大小写敏感: ...

  5. CentOS6.5升级手动安装GCC4.8.2 转载

    一.简易安装 操作环境 CentOS6.5 64bit,原版本4.4.7,不能支持C++11的特性~,希望升级到4.8.2 不能通过yum的方法升级,需要自己手动下载安装包并编译 1.1 获取安装包并 ...

  6. linux中为什么删除文件比创建文件要快,读取文件和删除文件的过程是什么?

    一.为什么删除文件比创建文件要快? 因为删除文件只是将bitmap位图表中将文件所占据的inode 和dacablock的使用状态从1变成0,相当于释放了这些快的使用权. 二.读取文件和删除文件的过程 ...

  7. 【POJ - 3009】Curling 2.0 (dfs+回溯)

    -->Curling 2.0 直接上中文 Descriptions: 今年的奥运会之后,在行星mm-21上冰壶越来越受欢迎.但是规则和我们的有点不同.这个游戏是在一个冰游戏板上玩的,上面有一个正 ...

  8. 使用mint ui 的picker解决城市三级联动

    <mt-popup v-model="popupVisible" position="bottom"> <div class="po ...

  9. 时间转换:DateTimeExtensions

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. mysql——插入、更新、删除数据(概念)

    一.插入数据 1.为表的所有字段插入数据 -------------------------------------------------------------------------- (1)i ...