The random number generator will cost no more than 500ms.
struct Rand{
unsigned int n,seed;
Rand(unsigned int n,unsigned int seed)
:n(n),seed(seed){}
int get(long long lastans){
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
return (seed^lastans)%n+1;
}
}; long long lastans=0,ans=0;
constexpr int P=998244353;
Rand rand(n,seed);
for(int i=0;i<m;i++){
int u=rand.get(lastans);
int v=rand.get(lastans);
int x=rand.get(lastans);
lastans=query(u,v);//calculate the answer
ans=(ans+lastans%P*x)%P;
}
//print 'ans' The first line containsintegers
.
is the number of houses.
is the number of queries.
The second line containsintegers. The
integer is
.
The third line containsintegers. The
integer
represents to the house marked
is connected to house marked
. It is guaranteed that the houses connect as a tree.
Then, you need to useand
to initialize the generator.
For the
query, you need to use the generator to get
integers
. And you should calculate the answer
. After that, use
to update
. You can refer to the second code in the description.
Only one integer,
.