ZOJ 3644 Kitty's Game(数论+DP)
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)的更多相关文章
- ZOJ 3644 Kitty's Game dfs,记忆化搜索,map映射 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的 ...
- 【bzoj1408】[Noi2002]Robot 数论+dp
题目描述 输入 输出 样例输入 3 2 1 3 2 5 1 样例输出 8 6 75 题解 语文题+数论+dp 花了大段讲述什么叫mu,什么叫phi,只是新定义的mu将2看作有平方因子,新定义的phi( ...
- 洛谷$P5366\ [SNOI2017]$遗失的答案 数论+$dp$
正解:数论$dp$ 解题报告: 传送门$QwQ$ 考虑先质因数分解.所以$G$就相当于所有系数取$min$,$L$就相当于所有系数取$max$ 这时候考虑,因为数据范围是$1e8$,$1e8$内最多有 ...
- zoj 3644(dp + 记忆化搜索)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...
- ZOJ 3494 (AC自动机+高精度数位DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3494 题目大意:给定一些被禁止的BCD码.问指定范围内不含有 ...
- ZOJ 1602 Multiplication Puzzle(区间DP)题解
题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...
- 数论+DP HDOJ 4345 Permutation
题目传送门 题意:一个置换群,经过最少k次置换后还原.问给一个N个元素,在所有的置换群里,有多少个不同的k. 分析:这道题可以转化成:N = Σ ai ,求LCM ( ai )有多少个不同的值.比如N ...
- POJ 3132 & ZOJ 2822 Sum of Different Primes(dp)
题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/show ...
- ZOJ 3802 Easy 2048 Again 状态DP
zoj 上次的月赛题,相当牛的题目啊,根本想不到是状态压缩好吧 有个预先要知道的,即500个16相加那也是不会超过8192,即,合并最多合并到4096,只有2的12次方 所以用状态压缩表示前面有的序列 ...
随机推荐
- form表单的一个页面多个上传按钮实例
/* * 图片上传 */ @RequestMapping("/uploadFile") @ResponseBody public String uploadFile(@Reques ...
- 解决 LLVM 错误提示 may only occur zero or one times!
使用 LLVM 混淆器添加参数进行编译提示如下错误:clang (LLVM option parsing): for the -bcf option: may only occur zero or o ...
- 网页股票期货历史数据(API)
//[和讯数据] //大商所DCE.郑商所CZCE.上期所SHFE3.中金所CFFEX //期货1分钟线http://webftcn.hermes.hexun.com/ ... I1709&d ...
- 树莓派3B+学习笔记:3、启用root账户
1.打开终端,输入 sudo passwd root 输入两次密码后设置root账户密码: 2.输入 sudo passwd --unlock root 解锁root账户: 3.点击主菜单的“Shut ...
- Home Assistant系列美化篇——替换天气 UI
替换天气组件 weather 的默认 UI,生成美观大方的气象卡片. Home Assistant 原生的天气平台不少,国内用户常用的有雅虎天气和 Darksky.其他论坛和社区也有分享自制的和风.彩 ...
- Qt上FFTW組件的编译与安裝
Qt上FFTW組件的編譯安裝 FFTW是一個做頻譜非常實用的組件,本文講述在Windows和Linux兩個平臺使用FFTW組件.Windows下的的FFTW組件已經編譯好成爲dll文件,按照開發應用的 ...
- 实验吧web加了料的报错注入
知识点: SQL注入中用到的Concat函数详解 http://www.sohu.com/a/219966085_689961 http分割注入 直接根据提示,提交post请求的用户名和密码 结 ...
- 009---linux进程管理
进程管理 top 查看运行状态:top 查看cpu核心数:top and 1 查看cpu占用率最大:top and P free 查看内存状态:free 以M为单位:free -m 以G为单位:fre ...
- Nodejs实战 —— 测试 Node 程序
读 <node.js实战2.0>,进行学习记录总结. 当当网购买链接 豆瓣网1.0链接 测试 Node 程序 本章内容 用 Node 的 assert 模块测试 使用其他断言库 使用 No ...
- 北京Uber优步司机奖励政策(9月28日~10月4日)
用户组:优步北京人民优步A组(适用于9月28日-10月4日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不 ...