Mr. Ant has 3 boxes and the infinite number of marbles. Now he wants to know the number of ways he can put marbles in these three boxes when the following conditions hold.
1) Each box must contain at least 1 marble.
2) The summation of marbles of the 3 boxes must be in between X and Y inclusive.
Now you are given X and Y. You have to find the number of ways Mr. Ant can put marbles in the 3 boxes.
Input
Input starts with an integer T, denoting the number of test cases. Each test case contains two integers X and Y.
Constraints
1<=T<=1000000
1<=X<= Y<=1000000
Output
For each test case, print the required answer modulo 1000000007.
Sample Input |
Sample Output |
1 4 5 |
9 |
Explanation for the first test case
1 | 1 | 2 |
Way 01
1 | 1 | 3 |
Way 02
1 | 2 | 1 |
Way 03
1 | 3 | 1 |
Way 04
2 | 1 | 1 |
Way 05
3 | 1 | 1 |
Way 06
1 | 2 | 2 |
Way 07
2 | 1 | 2 |
Way 08
2 | 2 | 1 |
Way 09
Note: use faster i/o method.
n个相同的东西放进三个不同的盒子里,每个盒子至少要有一个
这就是裸的排列组合题
用隔板法很容易知道,对于一个单独的n,答案就是C(n-1,2),令f(x)=C(x-1,2)
对于f(x)的一段求和,显然在n>=3时才有方案,即f(x)定义域x>=3
令g(x)=f(3)+f(4)+...+f(x),那么答案就是g(r)-g(l-1),(考虑到定义域应当是g(r)-g(l)+f(l)不过似乎不这样也行)
然后根据组合数的性质C(2,2)+C(3,2)+...+C(x-1,2)=C(x,3)
所以g(x)=C(x,3)
然后做完了
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 #define mod 1000000007 18 using namespace std; 19 inline LL read() 20 { 21 LL x=0,f=1;char ch=getchar(); 22 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 23 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 24 return x*f; 25 } 26 inline LL calc(LL a)//return C a 3 27 { 28 if (a<3)return 0; 29 return a*(a-1)*(a-2)/6%mod; 30 } 31 int main() 32 { 33 int T=read(); 34 while (T--) 35 { 36 LL a=read(),b=read(); 37 printf("%lld\n",(calc(b)-calc(a-1)+mod)%mod); 38 } 39 }
Spoj ANTP