SegmentTree
题号:NC237532
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

As everyone knows , segment tree is an elegant data structure .

One day , beginner Lucy started to learn segment tree and wrote the following code .


#include <bits/stdc++.h>
#define mid	(l + r) / 2
#define ls	now * 2
#define rs	now * 2 + 1
using namespace std;
int n, q, l, r;
int flag = 0;
void build( int now, int l, int r, int *a )
{
	if ( l == r )
	{
		cin >> a[now]; return;
	}
	build( ls, l, mid, a );
	build( rs, mid + 1, r, a );
	a[now] = a[ls] + a[rs];
	return;
}


int query( int now, int l, int r, int L, int R, int *a )
{
	if ( a[ls] + a[rs] != a[now] )
		flag = 1;
	if ( L <= l && r <= R )
		return(a[now]);
	int sum = 0;
	if ( L <= mid )
		sum += query( ls, l, mid, L, R, a );
	if ( mid + 1 <= R )
		sum += query( rs, mid + 1, r, L, R, a );
	return(sum);
}


int main()
{
	cin >> n >> q;
	int a[4 * n];
	memset( a, 0, sizeof(a) );
	build( 1, 1, n, a );
	for (int i = 1 ; i <= q ; i++ )
	{
		cin >> l >> r;
		cout << query( 1, 1, n, l, r, a ) << endl;
	}
	return 0 ;
}
Although this code made an mistake . But Lucy still passed T testcases and got AC .

Now we know n and q for each testcase , and each a_i is positive and smaller than 1000 .

Each query satisfied and completely randomly generated .

The question is , what's the probability of Lucy to get AC ?

输入描述:

The first line contains one integer  , indicates the number of testcases .

Then T lines , each line contains two integers n,q

输出描述:

If the answer is  (simplest fraction),print  .
示例1

输入

复制
2
8 1000
16 1000

输出

复制
1
示例2

输入

复制
1
13 1

输出

复制
857142864

说明

When n=8 or n=16 , the code is completely correct .