链接:https://www.nowcoder.com/acm/contest/147/D
来源:牛客网

Niuniu likes traveling. Now he will travel on a special graph.

Given k and n, The directed graph contains n vertices, which are numbered from 0 to n - 1.

For the vertex i, and for 1 <= j <= k, there is a directed edge from vertex i to vertex ((i + j) % n). 
We want to know the number of (directed) cycles, that pass each directed edge exactly once.
As the answer might be very large, you only need to output the answer mod 1000000007.

输入描述:

The first and only line contains two integers, which are k and n.
 
1 <= k <= 7
2k+1 <= n <= 109

输出描述:

The first and only line contains the answer.

输入例子:
2 5
输出例子:
11

-->

示例1

输入

2 5

输出

11

说明

The answer is not 22.
0 -> 1- > 2 -> 3 -> 4 -> 0 -> 2 -> 4 -> 1 -> 3 -> 0. 0 -> 2 -> 4 -> 1 -> 3 -> 0 -> 1 -> 2 -> 3 -> 4 -> 0. The two cycles are the same. They all passed the 10 edges. Only the start edges are different, and we think they are the same.

输入

3 8

输出

278528

解析   按照以上方式建立有向图 求欧拉回路的个数 。首先我们知道BEST定理 用矩阵求解欧拉回路的个数。但是题目给的点数太多,不能直接暴力

   每个点的出度和入度都为K   感觉应该是有什么规律,打个表怀疑可能是线性递推,试着下一下就是了。

 #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[][];
ll det(int n) {
ll 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] - (ll)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;
}
ll work(int k, int n) { //构造矩阵 计算点数为n的欧拉回路个数
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] = -;
}
}
ll t = ;
for (int i = ; i < k; i++) { //度数-1的阶乘
t = t * i % mod;
}
return (ll)det(n - ) * powmod(t, n) % mod; // 每个点的度数都一样 所以直接快速幂。
}
int main() {
int k;
ll n;
cin >> k >> n;
vector<int> a;
for (int i = * k + ; i <= ( << k)+ * k + ; i++) { //为什么是1<<k这么多项 也也不知道 题解说的。。。 正常写就在不超时的情况下尽量写大点呗
a.push_back(work(k, i));
}
cout << linear_seq::gao(a, n - ( * k + )) << endl;
return ;
}

牛客网暑期ACM多校训练营(第九场)D的更多相关文章

  1. 牛客网暑期ACM多校训练营 第九场

    HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...

  2. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  3. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  4. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  5. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  6. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

  7. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  8. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  9. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  10. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

随机推荐

  1. env - 在重建的环境中运行程序

    SYNOPSIS(总览) env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...] DESCRIPTION(描述) 设置环境中的每个NAME为VAL ...

  2. Dreamoon and MRT

    Dreamoon and MRT 题目链接: http://codeforces.com/group/gRkn7bDfsN/contest/212299/problem/B 只需要考虑相对位置,设a0 ...

  3. C++ new delete(一)

    在C#.Java這種managed語言,因為有garbage collection,所以完全不用考慮free()或delete,但在C/C++,有時候要delete的,有時又不用,到底哪些改delet ...

  4. JS数组专题1️⃣ ➖ 数组扁平化

    一.什么是数组扁平化 扁平化,顾名思义就是减少复杂性装饰,使其事物本身更简洁.简单,突出主题. 数组扁平化,对着上面意思套也知道了,就是将一个复杂的嵌套多层的数组,一层一层的转化为层级较少或者只有一层 ...

  5. 一套出完被喷爆的noip提高组+的题目

    这是一个悲伤的故事. 校内胡测嘛,这当然的重视啦,好好地出完题,看题面不是很难哦,那就用它吧. 结果今天老师考试就用上了(情况不妙) 果然考试过程中就有打喷嚏的冲动. 一道暴力,一道概率DP,一道主席 ...

  6. poj3710 Christmas Game

    题目描述 题解: 树上删边. 对于奇数长度的环,可以看做一条边. 对于偶数长度的环,可以看做什么都没有. 没有特别好的解释…… 代码: #include<cstdio> #include& ...

  7. Jenkins出现 {"changed": false, "msg": "mkdir: cannot create directory ‘/root/.ansible/tmp/ansible-tmp-1552390265.8-253945919613076’: No space left on device\n", "unreachable": true}

    之前部署Jenkins没有遇到过这个问题,百度也找不到相关内容,无奈用过google翻译了一下,显示内存不足,上到服务器查询df -h 发现use% 100% 由于日志等文件导致服务器磁盘空间满了,引 ...

  8. 上传文件到Ubuntu阿里云服务器(windows到Linux的文件上传)

    上传文件到Ubuntu阿里云服务器(windows到Linux的文件上传) 最近在阿里云上面租了一个轻量级服务器玩玩,学习学习怎么在服务器部署网站.然后嘞,在想要将本地文件上传到服务器的时候,自己研究 ...

  9. 转: 使用 /sys 文件系统访问 Linux 内核

    转一个挺不错的文章 使用 /sys 文件系统访问 Linux 内核 https://www.ibm.com/developerworks/cn/linux/l-cn-sysfs/ 如果你正在开发的设备 ...

  10. JS应用之正则表达式

    定义 正则表达式是用于匹配字符串中字符组合的模式. 创建正则表达式 两种方式: 1.new RegExp() let pattern1 = new RegExp('cat'); //第一个参数字符串 ...