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. 【CF1023B】Pair of Toys(解方程)

    题意:给定n个玩具要你选出两个玩具求出k的价值,第i个玩具的价值为i.若是没有选择方案,输出0 补充:玩具A与玩具B 和 玩具B和玩具A 是同一种选择 n,k<=1e14 思路:列出式子,解不等 ...

  2. gridview中的相关事件操作

    原文发布时间为:2008-07-27 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  3. 解决 ecshop 搜索特殊字符关键字(如:*,+,/)导致搜索结果乱码问题

    病症:ecshop系统搜索会对搜索关键字进行分词,然后对关键字分词进行正则匹配,并且标红加粗处理,如果关键字分词有特殊字符,则正则匹配结果会导致乱码 解决方法: 1.找到特殊字符串数组:$ts_str ...

  4. 饼系列—圈饼 doughnut

    chartControl属性 ApplicationSettings: Name:控件名字 AccessibleDescription: ??? AccessibleName:??? Accessib ...

  5. pycharm上传代码到码云错误现象用户密码

    >>出现此时错误说明在pycharm>git登录用户名或密码是错误的(必须填成是自己注册的码云邮箱和密码 不允许出现中文)并且无法在当前修改用户名或密码 >>接下来打开电 ...

  6. SRM1154--Topcoder初体验

    SRM 711 DIV2 <br > 在frank_c1的帮助下,辣鸡Xiejiadong也开始做Topcoder辣...... <br > 这算是一次Topcoder的初体验 ...

  7. rocketMq---------相关命令

    搭建就不详细说了,cent7.x的系统,openJdk8,maven3.x,gradle4.10.2, git 1.8.3.1 直接下载相关的二进制压缩包,解压即用,方便. 下面看常用的管理命令 ro ...

  8. Codeforces 401D Roman and Numbers

    题目大意 Description 给定一个数 N(N<1018) , 求有多少个经过 N 重组的数是 M(M≤100) 的倍数. 注意: ①重组不能有前导零; ②重组的数相同, 则只能算一个数. ...

  9. Excel文件处理Demo

    1.BLL业务逻辑代码 /// <summary> /// 处理“店铺竞品销售数据”导入文件 /// </summary> /// <param name="f ...

  10. [转] 常用SQL查询语句

    sunada  的原文地址 常用SQL查询语句 一.简单查询语句 1. 查看表结构 SQL>DESC emp; 2. 查询所有列 SQL>SELECT * FROM emp; 3. 查询指 ...