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. hdu 4418 高斯消元求期望

    Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu 1277 AC自动机

    全文检索 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. 【NOIP2016练习&BZOJ2125】T3 sp (树上倍增,最短路)

    题意:给一个N个点M条边的连通无向图,满足每条边最多属于一个环,有Q组询问,每次询问两点之间的最短路径. 对于80%的数据环的个数<=1 对于100%的数据N<=10000,Q<=1 ...

  4. [LeetCode] Text Justification words显示的排序控制

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  5. 12深入理解C指针之---指针多层间接引用

    该系列文章源于<深入理解C指针>的阅读与理解,由于本人的见识和知识的欠缺可能有误,还望大家批评指教. 一.指针多层引用 1.定义:指针可以用不同的间接引用层级,通常使用多重指针或字符数组来 ...

  6. 基于现有图像数据创建自定义像素格式的 BufferedImage

    在最近的一个项目中,需要实现 Mac OS X 环境下的摄像头图像实时捕获并转换为 Java 中的 BufferedImage 对象.首先通过开发一个本地库实现 Mac OS X 的摄像头图像捕获,采 ...

  7. TF-IDF 学习

    参考资料, 阮一峰的博客  http://www.ruanyifeng.com/blog/2013/03/tf-idf.html 非常感谢他, 能用如此通俗易懂的文字来阐述概念 TF -- Term ...

  8. Java集合——Collections工具类

    Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...

  9. see

    Description 问从点(0,0)能看到点(0,0)和(n,n)之间的矩形的多少个整数点,看到(x,y)代表点(0,0)和点(x,y)间没有其他整数点,如看不到(2,4)因为中间有点(1,2) ...

  10. IntelliJ IDEA重构技巧收集

    https://segmentfault.com/a/1190000002488608(重命名文件) http://www.jianshu.com/p/ab298b46bf50(快速生成方法) htt ...