B. Neural Network country
time limit per test

2 seconds

memory limit per test

256 megabytes

Due to the recent popularity of the Deep learning new countries are starting to look like Neural Networks. That is, the countries are being built deep with many layers, each layer possibly having many cities. They also have one entry, and one exit point.

There are exactly L layers, each having N cities. Let us look at the two adjacent layers L1 and L2. Each city from the layer L1 is connected to each city from the layer L2 with the traveling cost cij for , and each pair of adjacent layers has the same cost in between their cities as any other pair (they just stacked the same layers, as usual). Also, the traveling costs to each city from the layer L2are same for all cities in the L1, that is cij is the same for , and fixed j.

Doctor G. needs to speed up his computations for this country so he asks you to find the number of paths he can take from entry to exit point such that his traveling cost is divisible by given number M.

Input

The first line of input contains N (1 ≤ N ≤ 106), L (2 ≤ L ≤ 105) and M (2 ≤ M ≤ 100), the number of cities in each layer, the number of layers and the number that travelling cost should be divisible by, respectively.

Second, third and fourth line contain N integers each denoting costs 0 ≤ cost ≤ M from entry point to the first layer, costs between adjacent layers as described above, and costs from the last layer to the exit point.

Output

Output a single integer, the number of paths Doctor G. can take which have total cost divisible by M, modulo 109 + 7.

Example
input
2 3 13
4 6
2 1
3 4
output
2
Note

This is a country with 3 layers, each layer having 2 cities. Paths , and  are the only paths having total cost divisible by 13. Notice that input edges for layer cities have the same cost, and that they are same for all layers.

题意:

  给你一个起点,和一个终点

  中间这个图是L层的,每层到每层的每个点都有一条权值为b[i]的有向边

  起点到第一层每个点 也有一条权值为a[i]的有向边,最后一层每个点到终点也有一条权值为c[i]有向边,给出a,b,c,求出路径和能整除M的方案数

#include <bits/stdc++.h>
inline long long read(){long long x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;}
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const double pi = acos(-1.0);
const long long INF = 1e18+1LL; const int N = , mod = ; struct Matix {
LL arr[][];
}fi,se,ff; int n,L,M; Matix multi (Matix a, Matix b,int p) {
Matix ans;
memset(ans.arr,,sizeof(ans.arr));
if(p) {
for(int i = ; i < M; i++) {
for(int j = ; j < M; j++) {
for(int k = ; k < M; k++)
ans.arr[(i+j)%M][] += (a.arr[i][k] * b.arr[k][j])%mod,
ans.arr[(i+j)%M][] %= mod;
}
}
}
else {
for(int i = ; i < M; ++i) a.arr[i][] = a.arr[][i];
for(int i = ; i < M; i++) {
for(int j = ; j < M; j++) {
for(int k = ; k < M; k++)
ans.arr[][(i+j)%M] += (a.arr[i][k] * b.arr[k][j])%mod,
ans.arr[][(i+j)%M] %= mod;
}
}
}
return ans;
} Matix pows(Matix an,Matix a,LL x) {
while(x) {
if(x&) an=multi(an,a,);
a=multi(a,a,);
x/=;
}
return an;
}
int ar[N];
int main() {
cin >> n >> L >> M;
for(int i = ; i <= n; ++i) {
int x;
scanf("%d",&x);
fi.arr[x % M][] += ;
}
for(int i = ; i <= n; ++i) {
int x;
scanf("%d",&x);
se.arr[][x % M] += ;
ar[i] = x;
}
fi = pows(fi,se,L-);
memset(ff.arr,,sizeof(ff.arr));
for(int i = ; i <= n; ++i) {
int x;
scanf("%d",&x);
ff.arr[][(x+ar[i]) % M] += ;
}
fi = multi(fi,ff,);
LL ans = fi.arr[][];
printf("%lld\n",((ans)%mod+mod)%mod);
return ;
}

  

Bubble Cup X - Finals [Online Mirror] B. Neural Network country 矩阵快速幂加速转移的更多相关文章

  1. Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1

    Bubble Cup 12 - Finals Online Mirror, unrated, Div. 1 C. Jumping Transformers 我会状压 DP! 用 \(dp[x][y][ ...

  2. Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】

    Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...

  3. Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca

    题目链接: http://codeforces.com/contest/575/problem/B 题解: 把链u,v拆成u,lca(u,v)和v,lca(u,v)(v,lca(u,v)是倒过来的). ...

  4. Codeforces Bubble Cup 8 - Finals [Online Mirror]H. Bots 数学

    H. Bots Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/H Desc ...

  5. Codeforces Bubble Cup 8 - Finals [Online Mirror] D. Tablecity 数学题

    D. Tablecity Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/D ...

  6. Codeforces Bubble Cup 8 - Finals [Online Mirror] F. Bulbo DP

    F. Bulbo Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/F Des ...

  7. Bubble Cup X - Finals [Online Mirror]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 组了个菜鸡队打cf上的ACM比赛 比较快做完了8题但是菜的抠脚罚时巨多,所以最后被顶到了19名(居然没出首页) 自己的号自从上次疯狂掉分就没动 ...

  8. Bubble Cup 12 - Finals [Online Mirror, unrated, Div. 1] E. Product Tuples

    题意略,题解生成函数练习题,1+(q-ai)x卷积即可,线段树优化(类似分治思想) //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pra ...

  9. Bubble Cup 13 - Finals [Online Mirror, unrated, Div. 1] K. Lonely Numbers (数学)

    题意:定义两个数\(a,b\)是朋友,如果:\(gcd(a,b)\),\(\frac{a}{gcd(a,b)}\),\(\frac{b}{gcd(a,b)}\)能构成三角形,现在给你一个正整数\(n\ ...

随机推荐

  1. 【BZOJ4945&&UOJ317】游戏(2-sat,拓扑序)

    题意: 思路: 输出方案时有一个优秀的性质可以利用: tarjan缩点之后点所属的分量编号是原图的反的拓扑序 所以只需要在两种方案内找到所属分量编号较小的那个就行了,用来满足(i,i')那个限制 #i ...

  2. 标准C程序设计七---54

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  3. 13深入理解C指针之---内存管理

    该系列文章源于<深入理解C指针>的阅读与理解,由于本人的见识和知识的欠缺可能有误,还望大家批评指教. 内存管理对所有程序都很重要,主要包括显式内存管理和隐式内存管理.其中隐式内存管理主要是 ...

  4. Linux 之 FTP服务器搭建

    FTP服务器搭建 参考教程:[千峰教育] 1.关闭防火墙: service iptables stop 2.关闭Selinux setenforce 0 3.安装所需要依赖及编译工具 yum inst ...

  5. Cryptography I 学习笔记 --- 流密码

    1. 对于一次性密码本(one time pad),没有唯密文攻击(cypher text only attack),也就是说如果攻击者只能拿到密文,他什么也做不了 2. 完美密码:密钥长度大于密文长 ...

  6. Anaconda3中Python3.5和Python2.7共存

    开始-所有程序-Anaconda3-Anaconda Prompt conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/a ...

  7. GPP加密破解工具gpp-decrypt

    GPP加密破解工具gpp-decrypt   GPP是Group Policy Preferences(组策略首选项)的缩写,这是一个组策略实施工具.通过该工具,网络管理员可以实现更多的网络管理,如驱 ...

  8. tr命令用法

    原文链接 # echo "abcd"|tr 'a' 'b' bbcd tr 命令 用途 转换字符. 语法 tr [ -c | -cds | -cs | -C | -Cds | -C ...

  9. Java并发容器,底层原理深入分析

    ConcurrentHashMap ConcurrentHashMap底层具体实现 JDK 1.7底层实现 将数据分为一段一段的存储,然后给每一段数据配一把锁, 当一个线程占用锁访问其中一个段数据时, ...

  10. Linux内核源码分析--内核启动之zImage自解压过程

    参考: http://blog.chinaunix.net/uid-20543672-id-3018233.html Linux内核编译流程分析 linux2.6内核启动分析--李枝果(不看是你的损失 ...