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. 个人免签即时到账收款接口 bufpay.com 支持多账号收款

    有很多 bufpay 的用户反馈,单个手机收款有些时候不太方便,切换手机太麻烦:或者是营业额比较多,希望分摊到多个账号上面. 基于以上的问题,bufpay 开发了多手机收款的功能:每个收款的手机安装 ...

  2. 【HDOJ 5726】GCD(RMQ+二分)

    Problem Description Give you a sequence of N(N≤100,000) integers : a1,...,an(0<ai≤1000,000,000). ...

  3. 编程界失传秘术,SSO单点登录,什么是单点,如何实现登录?

    单点登录 多系统,单一位置登录,实现多系统同时登录的一种技术. 常出现在互联网应用和企业级平台中. 如:京东. 单点登录一般是用于互相授信的系统,实现单一位置登录,全系统有效的. 三方登录:某系统,使 ...

  4. 使用泛型与不使用泛型的Map的遍历

    https://www.cnblogs.com/fqfanqi/p/6187085.html

  5. 配置SpringBoot方便的切换jar和war

    配置SpringBoot方便的切换jar和war 网上关于如何切换,其实说的很明确,本文主要通过profile进行快速切换已实现在不同场合下,用不同的打包方式. jar到war修改步骤 pom文件修改 ...

  6. TinyMCE插件:RESPONSIVE filemanager 9 图片自动添加水印

    跟踪function() 搜索(filemanager/upload.php) 在代码中发现,上传成功后,会传回JSON信息数据,于是最后找到方法是 $upload_handler = new Upl ...

  7. vue-cli 3.0 使用axios配置跨域访问豆瓣接口

    vue-cli 3.0 配置axios跨域访问豆瓣接口 自己做的小demo 由于豆瓣api跨域问题,因此不能直接通过ajax请求访问,我们通过vue-cli提供给我们的代理 进行配置即可, 在根目录下 ...

  8. php bug 调试助手 debug_print_backtrace()

    debug_print_backtrace() 是一个很低调的函数,很少有人注意过它. 不过当我对着一个对象调用另一个对象再调用其它的对象和文件中的一个函数出错时,它也许正在一边笑呢 如果我们想知道某 ...

  9. php的基础知识(一)

    php(超文本预处理器)定义: php(外文名:php:Hertext Preprocessor,中文名:“超文本预处理器”)是一种通用的开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于 ...

  10. 详解 Python3 正则表达式(二)

    上一篇:详解 Python3 正则表达式(一) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 使用正则表达式 ...