zoukankan      html  css  js  c++  java
  • 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):

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <map>
     5 using namespace std;
     6 
     7 typedef long long LL;
     8 
     9 const int MAXN = 2010;
    10 const int MAXE = 20010;
    11 const int MOD = 1000000007;
    12 
    13 map<int, int> mp;
    14 
    15 int n, m, k, ecnt;
    16 int head[MAXN];
    17 int to[MAXE], next[MAXE];
    18 int dp[MAXN][MAXN], val[MAXN];
    19 
    20 void init() {
    21     memset(head, 0, sizeof(head));
    22     ecnt = 1;
    23 }
    24 
    25 inline void add_edge(int u, int v) {
    26     to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
    27 }
    28 
    29 LL gcd(LL a, LL b) {
    30     return b ? gcd(b, a % b) : a;
    31 }
    32 
    33 LL lcm(LL a, LL b) {
    34     return a * b / gcd(a, b);
    35 }
    36 
    37 inline void get_app() {
    38     mp.clear();
    39     int cnt = 0;
    40     for(int i = 1; i * i <= k; ++i) {
    41         if(k % i != 0) continue;
    42         mp[i] = ++cnt;
    43         if(i * i != k) mp[k / i] = ++cnt;
    44     }
    45 }
    46 
    47 int dfs(int u, LL x) {
    48     if(u == n && x == k) return 1;
    49     int now = mp[x];
    50     if(dp[u][now] != -1) return dp[u][now];
    51     dp[u][now] = 0;
    52     for(int p = head[u]; p; p = next[p]) {
    53         int &v = to[p];
    54         if(k % val[v] != 0) continue;
    55         LL tmp = lcm(x, val[v]);
    56         if(tmp == x || tmp > k) continue;
    57         dp[u][now] = (dp[u][now] + dfs(v, tmp)) % MOD;
    58     }
    59     return dp[u][now];
    60 }
    61 
    62 int main() {
    63     while(scanf("%d%d%d", &n, &m, &k) != EOF) {
    64         init();
    65         while(m--) {
    66             int u, v;
    67             scanf("%d%d", &u, &v);
    68             add_edge(u, v);
    69         }
    70         for(int i = 1; i <= n; ++i) scanf("%d", &val[i]);
    71         get_app();
    72         memset(dp, 255, sizeof(dp));
    73         int ans = (k % val[1]) ? 0 : dfs(1, val[1]);
    74         cout<<ans<<endl;
    75     }
    76 }
    View Code
  • 相关阅读:
    在某个点上弹出层
    根据表名、过程名、视图名查找对应的数据库
    js时间转换nan问题 兼容浏览器
    过滤html标记 以及 返回顶部
    自定义控件 加入include 报错 问题
    在有索引视图的表上新增、修改、删除 报错 set ARITHABORT 选项不对
    访微博代码
    兼容问题 链接不跳转
    js上下滚动代码
    onload问题
  • 原文地址:https://www.cnblogs.com/oyking/p/3255576.html
Copyright © 2011-2022 走看看