ZOJ 3644 Kitty's Game(DP)

Description

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

There are n scenes in the game(mark from 1 to n). Each scene has a number pi. Kitty‘s score will become least_common_multiple(x,pi) when Kitty enter the ith scene. x is
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(with p1 score). Please find out how many paths which can arrive at the nth scene and has k scores 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 integer n(2 ≤ n ≤ 2000), m(2 ≤ m ≤ 20000), k(2 ≤ k ≤ 106). Then followed by m lines. Each line contains two integer uv(1
≤ uv ≤ n, u ≠ v) indicate we can go to vth scene from uth scene directly. The last line of each case contains n integer pi(1 ≤pi ≤ 106).

Process to the end of input.

Output

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

Sample Input

5 6 84
1 2
2 5
1 3
3 5
1 4
4 5
1 5 4 12 21

Sample Output

2

马丹,map不太会用,看的别人的
题意:n:n个点 m:m条路  k:值,每个点有一个值,要求从起点到终点的不同方案。中间不用许出现LCM不变的情况

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int maxn=2010;
const int MOD=1e9+7;
vector<LL>v[maxn];
map<LL,LL>M;
LL dp[maxn][maxn],p[maxn],k;
int n,m;
LL gcd(LL a,LL b)
{
    return b?gcd(b,a%b):a;
}
LL lcm(LL a,LL b)
{
    return a/gcd(a,b)*b;
}
LL dfs(LL u,LL s)
{
//    cout<<"madan  "<<u<<endl;
    int num=M[s];
    if(dp[u][num])   return dp[u][num];
    LL ret=0;
    for(int i=0;i<(int)v[u].size();i++)
    {
        LL ed=v[u][i];
        LL LCM=lcm(s,p[ed]);
        if(k%LCM||LCM==s&&u!=0)  continue;
        ret+=dfs(ed,LCM)%MOD;
    }
    return dp[u][num]=ret;
}
int main()
{
    LL st,ed;
    while(~scanf("%d%d%lld",&n,&m,&k))
    {
        REPF(i,0,n)  v[i].clear();
        while(m--)
        {
            scanf("%lld%lld",&st,&ed);//wa了一个小时
            v[st].push_back(ed);
        }
        REPF(i,1,n)   scanf("%lld",&p[i]);
        if(k%p[n])
        {
            puts("0");
            continue;
        }
        M.clear();
        v[0].push_back(1);
        int cnt=0;
        for(LL i=1;i*i<=k;i++)
        {
            if(k%i==0)
            {
                M[i]=cnt++;
                M[k/i]=cnt++;
            }
        }
        memset(dp,0,sizeof(dp));
        dp[n][M[k]]=1;
        printf("%lld\n",dfs(0,1));
    }
    return 0;
}

ZOJ 3644 Kitty's Game(DP)

时间: 2024-08-05 15:51:02

ZOJ 3644 Kitty's Game(DP)的相关文章

ZOJ 3644 Kitty&#39;s Game (图上DP 约数)

哎-这一场就做了三个题目,全队倒数第一,简直是太弱了. A Kitty's Game (ZOJ 3644) 题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3644 题意: 给出一个有向图,每个节点有一个权值pi, 有一个人从1节点出发(其权值为1节点的权值),前往n号节点,每经过一个节点,他的权值就变成了他经过这个节点前的权值和这个节点权值的最小公倍数,如果他经过这个节点后权值不发生变化则他就不能经过这个节点

ZOJ 3644 Kitty&#39;s Game dfs,记忆化搜索,map映射 难度:2

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的因数,所以不会形成环,只需从1直接走,走到n即可. 但是如果这样的话时空复杂度就都是nk,明显不满足题意,而这个时候我们可以想到,每个状态都必然是k的约数,(点数不是k的约数的节点不在路上,可以无视),而约数的个数也就k^0.5个,可以直接用map映射,这样时空复杂度都是n*k^0.5,可以解出答案

ZOJ 1743 Concert Hall Scheduling(DP)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=743 题意:有两个音乐厅出租.给出n个租客,每个租客有个租的时间段[L,R],以及租费.任意时候音乐厅只能租给最多一个租客.问如何选择租给哪些租客使得赚的钱最多? 思路:f[i][j]表示第一个音乐厅到时刻i.第二个到时刻j,可以获得的最大值. struct node { int x,y,w; int operator<(const node &a) const

ZOJ 1074 To the Max (DP)

Problem Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In

ZOJ 1642 Match for Bonus(dp)

Match for Bonus Time Limit: 2 Seconds      Memory Limit: 65536 KB Roy played a game with his roommates the other day. His roommates wrote 2 strings of characters, and gave each character a bonus value. When Roy pinned the positions of one common char

ZOJ 3631 Watashi&#39;s BG(dp+dfs)

题意:一共要吃n顿饭 公款m元 如果公款大于等于饭局所需费用 就全用公款 如果小于就自费 求最后能用的公款为多少 思路: dfs(i - 1, val + dp[i]); dfs(i - 1, val); #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; int dp[50]; int n, m; int a

zoj 3706 Break Standard Weight(dp)

Break Standard Weight Time Limit: 2 Seconds                                     Memory Limit: 65536 KB The balance was the first mass measuring instrument invented. In its traditional form, it consists of a pivoted horizontal lever of equal length ar

zoj 3812 We Need Medicine (dp 位优化 巧妙记录路径)

We Need Medicine Time Limit: 10 Seconds      Memory Limit: 65536 KB      Special Judge A terrible disease broke out! The disease was caused by a new type of virus, which will lead to lethal lymphoedema symptom. For convenience, it was namedLL virus.

ZOJ 3605 Find the Marble(dp啊 三维)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4708 Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts, Alice puts the pots in one line and puts the marble in one of