5091: [Lydsy0711 month] pick Apple time limit:1 Sec Memory limit:256 MB
submit:148 solved:114
[Submit] [Status] [Discuss] Description Little Q's job is picking apples in the garden. In the garden there are n apple trees and M two-way road, the apple tree numbered 1 to N, the two ends of each road are connected with two different apple trees. Let's say the apple tree is connected to the D_i road. The little Q will pick apples in the following ways: 1. Small q is randomly moved under an apple tree, and the probability of moving to the apple tree under the I Tree is d_i/(2m), but not picked here. 2. Randomly select a path that is connected to the current Apple tree and move it under another apple tree. 3. Assuming that it is currently under the apple tree, he will pick a_i apples and repeatedly pick them under the same apple tree. 4. Repeat steps 2nd and 3 K times. Please write a program to help calculate how many apples small Q expects to pick. Input
The first line contains three positive integer n,m,k (n,k<=100000,m<=200000), each representing the number of apple trees and roads and the number of repeat steps.
The second line contains n positive integers, which in turn represent a_1,a_2,..., A_n (1<=a_i<=100). The next m line, two positive integers per line u,v (1<=u,v<=n,u!=v), indicates that there is a road between the first and second apple trees. Output
If the answer is p/q, the output is an integer, p*q^{-1} mod 1000000007 (10^9+7).
Sample Input3 4 2
2 3 4
1 2
1 2
2 3
3 1Sample Output750000011
Expected for 5.75=23/4= (23*250000002) mod 1000000007=750000011. let's set f[i][j] for the probability of going to J after I have walked. obviously f[0][j] = d[j]/2m. then the answer is ΣΣF[I][J] * A[j]But in fact no matter how much I is, f[i][j] is equal to d[j]/2m, next I will prove this point. because f[0][j] equals d[j]/2m, so the probability of our first walk on each edge (with 2m edges in mind) is 1/2m,so the probability of each point being taken is d[j]/2m, i.e. f[1][j] = F[0][j]. and then it came out, 2333.
#include <bits/stdc++.h> #define LL long longusing namespace std;const int Maxn=200005;const int Ha=1000000007;int Inv,n,m,ans;int k,d[maxn],a[maxn];inline int Add (int x,int y) {X+=y;return x>=ha?x-ha:x;} inline int ksm (int x,int y) {int an=1;for (; y;y>>=1,x=x* (LL) x%ha) if (y&1) an=an* (LL) x%ha;return an;} int main () { scanf ("%d%d%d", &n,&m,&k); int UU,VV; INV=KSM (2*m,ha-2); for (int i=1;i<=n;i++) scanf ("%d", a+i); for (int i=1;i<=m;i++) { scanf ("%d%d", &UU,&VV); d[uu]++,d[vv]++; } for (int i=1;i<=n;i++) Ans=add (ans,a[i]*d[i]* (LL) inv%ha); ans=ans* (ll) K%ha; printf ("%d\n", ans); return 0;}
Bzoj 5091: [Lydsy0711 month] pick apples