HDU-4812:D Tree(点分治)

D Tree
Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 102400/102400 K (Java/Others)

Problem Description
There is a skyscraping tree standing on the playground of Nanjing University of Science and Technology. On each branch of the tree is an integer (The tree can be treated as a connected graph with N vertices, while each branch can be treated as a vertex). Today the students under the tree are considering a problem: Can we find such a chain on the tree so that the multiplication of all integers on the chain (mod 1 0 6 + 3 10^6 + 3 106+3) equals to K?
Can you help them in solving this problem?

Input
There are several test cases, please process till EOF.
Each test case starts with a line containing two integers N ( 1 < = N < = 1 0 5 ) N(1 <= N <= 10^5) N(1<=N<=105) and K ( 0 < = K < 1 0 6 + 3 ) K(0 <=K < 10^6 + 3) K(0<=K<106+3). The following line contains n numbers v i ( 1 < = v i < 1 0 6 + 3 ) v_i(1 <= v_i < 10^6 + 3) vi(1<=vi<106+3), where vi indicates the integer on vertex i. Then follows N − 1 N - 1 N1 lines. Each line contains two integers x and y, representing an undirected edge between vertex x and vertex y.

Output
For each test case, print a single line containing two integers a and b (where a < b), representing the two endpoints of the chain. If multiply solutions exist, please print the lexicographically smallest one. In case no solution exists, print “No solution”(without quotes) instead.
For more information, please refer to the Sample Output below.

Sample Input
5 60
2 5 2 3 3
1 2
1 3
2 4
2 5
5 2
2 5 2 3 3
1 2
1 3
2 4
2 5

Sample Output
3 4
No solution

思路:点分治。

同时将 a ∗ b = k a*b=k ab=k变换为 a = k b % ( 1 0 6 + 3 ) a=\frac kb\%(10^6+3) a=bk%(106+3),求逆元。然后对于b,判断是否存在 a = k ∗ b − 1 % ( 1 0 6 + 3 ) a=k*b^{-1}\%(10^6+3) a=kb1%(106+3)

#include
using namespace std;
const int MOD=1e6+3;
const int MAX=1e6+10;
typedef long long ll;
ll POW(ll x,int n){ll res=1;while(n){if(n&1)res=res*x%MOD;x=x*x%MOD;n/=2;}return res;}
vectore[MAX];
int v[MAX],son[MAX];
int cnt[MAX],d[MAX];
int inv[MAX];
int root,sum;
void getroot(int k,int fa)
{
    son[k]=1;
    cnt[k]=0;
    for(int i=0;ik)ma[1ll*d[k]*x%MOD]=k;
    for(int i=0;ik)ma[a[k]]=k;
    for(int i=0;i

你可能感兴趣的:(分治)