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次方 所以用状态压缩表示前面有的序列 ...
随机推荐
- Mysql存储引擎myisam与inndb的区别?
最近在研究Mysql存储引擎这块,说白了就是如何存储数据.如何为存储的数据建立索引和如何更新.查询数据等技术的实现方法,在此做一个大概总结: 其实在工作中用的最多也就是MYISAM和INNODB,IN ...
- linux 网络服务之一
- Spark_安装配置_运行模式
一.Spark支持的安装模式: 1.伪分布式(一台机器即可) 2.全分布式(至少需要3台机器) 二.Spark的安装配置 1.准备工作 安装Linux和JDK1.8 配置Linux:关闭防火墙.主机名 ...
- 对QP中RTC的理解
1.概念 RTC(Run To Completion)是运行到完成为止的意思.在状态机中,从源状态到目标状态的转换动作要运行到完成. 从字面上来看,这个过程像是不可中断的,但实际并不是,这个过程可以被 ...
- URL和报文知识总结 ——1
第一部分:浏览器生成的消息 关键词:URL的解析 HTTP请求的生成 DNS服务器 1.URL(网址)的构成: 对于一个网址的解析: https://i.cnblogs.com/index.htm ...
- 快排(golang实现) 递归方法
递归方法,逻辑简洁清晰.这个算法还是很重要的,需要重点记忆理解,面试经常考手写.据说是与傅里叶变换等并称“20世纪十大算法”.https://blog.csdn.net/v_JULY_v/articl ...
- 24-集成ASP.NETCore Identity采用EF
1-增加IdentityServer4.AspNetIdentity nuget包 2- StartUp.cs启用增加相应的代码 .AddAspNetIdentity<ApplicationU ...
- Sql Server char、varchar、nchar、nvarchar的区别
(1) 定义: char: 固定长度,存储ANSI字符,不足的补英文半角空格. nchar: 固定长度,存储Unicode字符,不足的补英文半角空格 varchar: 可变长度,存储ANSI字符,根据 ...
- 封装Excls数据导出功能 返回一个下载链接地址
/// <summary> /// 获取本地存储地址 /// </summary> /// <param name="dt"></para ...
- java操作HDFS
package com.lei.hadoop; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Fil ...