Spoj-ANTP Mr. Ant & His Problem

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

时间: 2024-10-25 20:04:10

Spoj-ANTP Mr. Ant & His Problem的相关文章

HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 312    Accepted Submission(s): 219 Problem Description One day, you, a clever boy, feel bored in your math class, and then fall

HDU5924 Mr. Frog’s Problem

1 /* 2 HDU5924 Mr. Frog's Problem 3 http://acm.hdu.edu.cn/showproblem.php?pid=5924 4 数论 5 * 6 */ 7 #include <cstdio> 8 int main() 9 { 10 long long a,b; 11 int t; 12 scanf("%d",&t); 13 for(int k=1;k<=t;k++) 14 { 15 scanf("%lld%l

SPOJ:Another Longest Increasing Subsequence Problem(CDQ分治求三维偏序)

Given a sequence of N pairs of integers, find the length of the longest increasing subsequence of it. An increasing sequence A1..An is a sequence such that for every i < j, Ai < Aj. A subsequence of a sequence is a sequence that appears in the same

【网络流补全计划】Part.Ⅲ有上下界的网络流

本来心情就非常糟糕调月下毛景树把我最后一点写代码的心情调没了 放弃 开始补全网络流. 之前学了普通最大流,那么现在开始补有上下界的网络流. 在普通最大流中,网络里的每一条边都只有流量的上界即边的容量,而引入上下界网络流之后,每个边不但有一个容量,还有一个流量下界. 我们令B(u,v)表示边(u,v)的下界.于是我们可以有表达式: B(u,v)≤f(u,v)≤C(u,v) 有这个式子可以得到 0≤f(u,v)≤C(u,v)?B(u,v) 至此,我们可以将有上下界的网络流分为几种问题来对待,接下来就

暑假集训-个人赛第四场

ID Origin Title   10 / 52 Problem A SPOJ AMR10A Playground     Problem B SPOJ AMR10B Regex Edit Distance     Problem C SPOJ AMR11C Robbing Gringotts   1 / 14 Problem D SPOJ AMR10D Soccer Teams   0 / 3 Problem E SPOJ AMR10E Stocks Prediction   17 / 19

10.6 CCPC northeast

1001 Minimum’s Revenge 1002 Prediction 1003 Mr. Frog’s Problem 1004 Coconuts 1005 Mr. Frog’s Game 1006 Auxiliary Set 1007 Birthday Gift 1008 Basic Data Structure 1009 GCD 1010 Mission Possible 1011 Backpack on Tree

The New Villa (Uva 321 bfs)

The New Villa Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Mr. Black recently bought a villa in the countryside. Only one thing bothers him: although there are light switches in most rooms, the l

codeforces Round 286# problem A. Mr. Kitayuta&#39;s Gift

Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked to insert exactly one lowercase English letter into s to make it a palindrome. A palindrome is a string that reads the same forward and backward. For

Ant Problem: failed to create task or type foreach 问题

用eclipse导出android时总是会出现有类没有导出的现象,感觉非常麻烦,就用ant些了脚本.在eclipse中运行脚本没问题.可是在命令行下运行会出现 Problem: failed to create task or type foreach问题.出问题的脚本是 <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${path.build}/ant-contrib.jar