Description

Kitty is a little cat. She is crazy about a game recently.

There arenscenes in the game(mark from 1 ton). Each scene has a numberpi. Kitty's score will become least_common_multiple(x,pi) when Kitty enter theithscene.xis the score that Kitty had previous. Notice that Kitty will become mad If she go to another scene but the score didn't change.

Kitty is staying in the first scene now(withp1score). Please find out how many paths which can arrive at thenthscene and haskscores at there. Of course, you can't make Kitty mad.

We regard two paths different if and only if the edge sequence is different.

Input

There are multiple test cases. For each test case:

The first line contains three integern(2 ≤n≤ 2000),m(2 ≤m≤ 20000),k(2 ≤k≤ 106). Then followed bymlines. Each line contains two integeru,v(1 ≤u,v≤ n, u ≠ v) indicate we can go tovthscene fromuthscene directly. The last line of each case contains n integerpi(1 ≤pi≤ 106).

Process to the end of input.

Output

One line for each case. The number of paths module 1000000007.

题目大意:有n个点m条无向边,每个点有一个分值p[i],设x为当前分值,每经过一个点,分值就会变成LCM(x, p[i]),若经过一个点的时候分值没有变化,那么这个点不能走。现在要从点1走到点n,要得到k的分值,问有多少种方法。

思路:首先走的每一步都必须是k的约数(不然到达终点的时候不可能得到k),那么走到每个点上至多有sum{k的约数}的分值,把k离散化,k最多有2*sqrt(k)个约数,即2000个。然后开始DP,dp[i][j]表示走到第i个点,得到分值j(离散值),并且能走到终点的方案数。然后记忆化搜索即可。原图有环也无所谓,因为分值每走一步必须要变化,而且只会越来越大,不会走出环来。

代码(120MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std; typedef long long LL; const int MAXN = ;
const int MAXE = ;
const int MOD = ; map<int, int> mp; int n, m, k, ecnt;
int head[MAXN];
int to[MAXE], next[MAXE];
int dp[MAXN][MAXN], val[MAXN]; void init() {
memset(head, , sizeof(head));
ecnt = ;
} inline void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
} LL gcd(LL a, LL b) {
return b ? gcd(b, a % b) : a;
} LL lcm(LL a, LL b) {
return a * b / gcd(a, b);
} inline void get_app() {
mp.clear();
int cnt = ;
for(int i = ; i * i <= k; ++i) {
if(k % i != ) continue;
mp[i] = ++cnt;
if(i * i != k) mp[k / i] = ++cnt;
}
} int dfs(int u, LL x) {
if(u == n && x == k) return ;
int now = mp[x];
if(dp[u][now] != -) return dp[u][now];
dp[u][now] = ;
for(int p = head[u]; p; p = next[p]) {
int &v = to[p];
if(k % val[v] != ) continue;
LL tmp = lcm(x, val[v]);
if(tmp == x || tmp > k) continue;
dp[u][now] = (dp[u][now] + dfs(v, tmp)) % MOD;
}
return dp[u][now];
} int main() {
while(scanf("%d%d%d", &n, &m, &k) != EOF) {
init();
while(m--) {
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v);
}
for(int i = ; i <= n; ++i) scanf("%d", &val[i]);
get_app();
memset(dp, , sizeof(dp));
int ans = (k % val[]) ? : dfs(, val[]);
cout<<ans<<endl;
}
}

ZOJ 3644 Kitty's Game(数论+DP)的更多相关文章

  1. ZOJ 3644 Kitty's Game dfs,记忆化搜索,map映射 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的 ...

  2. 【bzoj1408】[Noi2002]Robot 数论+dp

    题目描述 输入 输出 样例输入 3 2 1 3 2 5 1 样例输出 8 6 75 题解 语文题+数论+dp 花了大段讲述什么叫mu,什么叫phi,只是新定义的mu将2看作有平方因子,新定义的phi( ...

  3. 洛谷$P5366\ [SNOI2017]$遗失的答案 数论+$dp$

    正解:数论$dp$ 解题报告: 传送门$QwQ$ 考虑先质因数分解.所以$G$就相当于所有系数取$min$,$L$就相当于所有系数取$max$ 这时候考虑,因为数据范围是$1e8$,$1e8$内最多有 ...

  4. zoj 3644(dp + 记忆化搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...

  5. ZOJ 3494 (AC自动机+高精度数位DP)

    题目链接:  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3494 题目大意:给定一些被禁止的BCD码.问指定范围内不含有 ...

  6. ZOJ 1602 Multiplication Puzzle(区间DP)题解

    题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...

  7. 数论+DP HDOJ 4345 Permutation

    题目传送门 题意:一个置换群,经过最少k次置换后还原.问给一个N个元素,在所有的置换群里,有多少个不同的k. 分析:这道题可以转化成:N = Σ ai ,求LCM ( ai )有多少个不同的值.比如N ...

  8. POJ 3132 &amp; ZOJ 2822 Sum of Different Primes(dp)

    题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/show ...

  9. ZOJ 3802 Easy 2048 Again 状态DP

    zoj 上次的月赛题,相当牛的题目啊,根本想不到是状态压缩好吧 有个预先要知道的,即500个16相加那也是不会超过8192,即,合并最多合并到4096,只有2的12次方 所以用状态压缩表示前面有的序列 ...

随机推荐

  1. WSO2 API Manager 替换mysql作为数据库,解决AuthorizationUtils Could not set authorizations for the root问题

    按照wso2官网(https://docs.wso2.com/display/ADMIN44x/Changing+to+MySQL)配置AM的数据库,想从H2换成Mysql5.7,费了将近一天的时间, ...

  2. MySQL高可用之MGR安装测试

    Preface     We've learned the machenism of MGR yesterday,Let's configurate an environment and have s ...

  3. Python豆瓣源

    pip install -i https://pypi.doubanio.com/simple/ xxxx

  4. jQuery实现 自动滚屏操作

    实现自动滚屏思路: 1.滚屏即:文本的往上移动一段距离: 2.那么我们使文本每过一段时间就往上移动一段固定距离,就可实现滚屏: 3.直到文本底部出现在浏览器窗口中,专业点就是 文本移动的距离 + 浏览 ...

  5. wso2 ei 6.4.0安装笔记

    目的:将最新版(6.4.0)部署在linux服务器,与Api Manager部署在同一环境 环境: Centos 7.3 Jdk 8 Mysql 5.7 问题一: 将H2替换为Mysql5.7数据库时 ...

  6. Leecode刷题之旅-C语言/python-206反转链表

    /* * @lc app=leetcode.cn id=206 lang=c * * [206] 反转链表 * * https://leetcode-cn.com/problems/reverse-l ...

  7. java语言描述 猴子吃桃问题(递归和循环写法)

    //题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个//第二天早上又将剩下的桃子吃掉一半,又多吃了一个//以后每天早上都吃了前一天剩下 的一半零一个.到第10天早上想再 ...

  8. while do while switch语句的简要分析

    1 //// while是C语言的一个关键字,其后是使用一个小括号中的条件表达式来做为执行循环的条件, 2 ////也就是说当条件表达式的结果为真时执行大括号里面的的程序内容, 3 ////而当条件表 ...

  9. C#/STM32 WAV转byte WAV数据格式

    最近在做STM32音乐播放器,选取了最容易做的WAV格式. 为了更方便开发自己做了一个WAV转Byte的小上位机 附软件下载链接 链接:https://pan.baidu.com/s/1Zz7bczZ ...

  10. F. Make It Connected

    题目链接:http://codeforces.com/contest/1095/problem/F 题意:给你n个点,每个点有个权值,如果在两点之间添一条边,代价为两点权值之和.现在给出m个边可以选择 ...