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. 学习javascript设计模式之中介者模式

    1.中介者模式的作用就是解除对象与对象之间的紧耦合关系.增加一个中介者对象后,所有的相关对象都通过中介者来通信,而不是互相引用,所以当一个对象发生改变时,只需要通知中介对象即可.中介者使各对象之间耦合 ...

  2. 【Visual Studio】Error: forget to add '#include "stdafx.h"' to your source (转)

    原文转自 http://www.cnblogs.com/qunews/articles/2200313.html [问题原因]在编译时使用了预编译头文件, [解决方法]Project-->Pro ...

  3. 安装apache2.4和php7.1的方法记录

    下载扩展 这是PHP官方扩展下载的地址 http://pecl.php.net 点击下载dll文件,放到php的ext目录. 看这个说明下载你PHP版本支持的版本.

  4. python常用模块2

    collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict. ...

  5. (5)Unity3d GUI

  6. [原创][FPGA]Quartus中调用Modelsim波形仿真步骤说明

    0. 简介 在使用QuartusII软件的过程中,经常地需要跑仿真,那么说到仿真就不得不说Modelsim这个仿真软件了,我们这里介绍下该软件在QuartusII中的使用方法. 1. 建立Quartu ...

  7. Extjs grid 单元格事件

    celldblclick: function (view, td, cellIndex, record, tr, rowIndex, e, eOpts) { //extjs 4.2下,有时出现,多次不 ...

  8. react/redux组件库、模板、学习教程

    开源的有蚂蚁金服的: 1.https://pro.ant.design/index-cn 2.https://pro.ant.design/docs/getting-started-cn 3.http ...

  9. Linux 软件大全

    应用 音频 Airtime - Airtime 是一款用于调度和远程站点管理的开放广播软件   Ardour - 在 Linux 上录音,编辑,和混音  Audacious - 开源音频播放器,按你想 ...

  10. 【Nginx】开发一个HTTP过滤模块

    与HTTP处理模块不同.HTTP过滤模块的工作是对发送给用户的HTTP响应做一些加工. server返回的一个响应能够被随意多个HTTP过滤模块以流水线的方式依次处理.HTTP响应分为头部和包体,ng ...