Ralph and Mushrooms

题目链接:http://codeforces.com/contest/894/problem/E

Description

Ralph is going to collect mushrooms in the Mushroom Forest.

There are m directed paths connecting n trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic speed. New mushrooms regrow as soon as Ralph finishes mushroom collection on a path. More specifically, after Ralph passes a path the i-th time, there regrow i mushrooms less than there was before this pass. That is, if there is initially x mushrooms on a path, then Ralph will collect x mushrooms for the first time, x - 1 mushrooms the second time, x - 1 - 2 mushrooms the third time, and so on. However, the number of mushrooms can never be less than 0.

For example, let there be 9 mushrooms on a path initially. The number of mushrooms that can be collected from the path is 9, 8, 6 and 3 when Ralph passes by from first to fourth time. From the fifth time and later Ralph can't collect any mushrooms from the path (but still can pass it).

Ralph decided to start from the tree s. How many mushrooms can he collect using only described paths?

Input

The first line contains two integers n and m \((1 ≤ n ≤ 10^6, 0 ≤ m ≤ 10^6)\), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively.

Each of the following m lines contains three integers x, y and w \((1 ≤ x, y ≤ n, 0 ≤ w ≤ 10^8)\), denoting a path that leads from tree x to tree y with w mushrooms initially. There can be paths that lead from a tree to itself, and multiple paths between the same pair of trees.

The last line contains a single integer s (1 ≤ s ≤ n) — the starting position of Ralph.

Output

Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.

Sample Input and Output

Sample InputSample Output
2 2
1 2 4
2 1 4
1
6
3 3
1 2 4
2 3 3
1 3 8
1
8

Hint

In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect.

In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.


题意

要求构造NxM的矩阵,使得每一行的乘积均为k,每一列的乘积均为k,其中k只可能是-1和1

题解

首先,这个矩阵肯定只能是1和-1组成的矩阵了
若k为1,那么每一列只能有偶数个-1;若k为-1,那么每一列只能有奇数个-1
那么对于每一列(长度为N),都有\(2^{N-1}\)种可能的选择
那么,假设前\(M-1\)列都构造完毕,最后一列呢?
显然,如果第1行已经有x个-1,那么最后一列的第1行应该是一个使得该行有奇数个-1(对应k=-1)/偶数个-1(对应k=1)的数,显然也可以确定
这样,整个矩阵都可以构建完成,可见这个矩阵的确定只与前M-1列有关,一共有 \((2^{N-1})^{M-1}\)个矩阵
那么问题来了:N行的乘积为k了,前M-1列的乘积也为k了,那么第M列的乘积呢?
讨论如下:
k=1:若前M-1列构成的矩阵第i行乘积为-1,那么第M列第i行也是-1;若为1,那么第i行也是1;既然如此,那么说明第M列元素的乘积等于前M-1列的所有行的乘积,后者等价于所有列的乘积,故第M列的乘积为1
k=-1:若前M-1列构成的矩阵第i行乘积为-1,那么第M列第i行是1;若为1,那么第i行是-1;既然如此,那么说明第M列元素的乘积等于前M-1列的所有 [行的乘积的相反数]
的乘积,后者等于所有数的乘积再乘以\((-1)^{N}\)(N为行数),等价于前M-1列的乘积乘上\((-1)^{N}\),也就是说最后一列的乘积为\((-1)^{M-1}\times (-1)^{N}\)
若\(N\times (M-1)\)为偶数,最后一列乘积为1
若\(N\times (M-1)\)为奇数,最后一列乘积为-1
其余结论显然

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MOD = 1000000007;

ll m,n;
ll quickPow(ll k)
{
    ll ans=1,now=2;
    while(k)
    {
        if(k&1) ans*=now,ans%=MOD;
        now*=now,now%=MOD;
        k>>=1;
    }
    return ans;
}

int main()
{
    int k;
    cin>>m>>n>>k;
    if(k==-1&&(m+n)%2)
    {
        cout<<"0"<<endl;
        return 0;
    }
    m--,n--;
    m%=(MOD-1);
    n%=(MOD-1);
    ll t=m*n%(MOD-1);
    cout<<quickPow(t)<<endl;
    return 0;
}

版权所有:scidylanpno
原文链接:http://www.cnblogs.com/scidylanpno/p/7978086.html