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)

然后做完了

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
#define mod 1000000007
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline LL calc(LL a)//return C a 3
{
if (a<)return ;
return a*(a-)*(a-)/%mod;
}
int main()
{
int T=read();
while (T--)
{
LL a=read(),b=read();
printf("%lld\n",(calc(b)-calc(a-)+mod)%mod);
}
}

Spoj ANTP

最新文章

  1. 父子页面之间元素相互操作(iframe子页面)
  2. 信息安全比赛总结(21ic转帖)
  3. Files 的值“&lt;&lt;&lt;&lt;&lt;&lt;&lt; .mine”无效。路径中具有非法字符
  4. 使用GDB 追踪依赖poco的so程序,core dump文件分析.
  5. php页面静态化
  6. Memcached的安装和使用以及nginx整合memcached
  7. NOI模拟赛Day3
  8. Linux Shell脚本入门--cut命令
  9. HDOJ-ACM1010(JAVA) 奇偶剪枝法 迷宫搜索
  10. 基本数据类型的常量池与String类型常量池解析
  11. 用Winrar批量解压缩有密码文件方法,只需输入一次密码
  12. 【转】Java 并发:Executors 和线程池
  13. display: run-in
  14. 【图文详解】linux下配置远程免密登录
  15. Project facet Java version 1.8 not supported JDK版本不对无法启动项目解决办法
  16. Qt Creator 中文编译失败 怎么办
  17. E - Emptying the Baltic Kattis - emptyingbaltic (dijkstra堆优化)
  18. Android Studio 使用USB真机调试教程
  19. npm安装时一些错误
  20. 【Html】Vue动态插入组件

热门文章

  1. js3
  2. ajax上传文件以及使用中常见问题处理
  3. COGS 2688. 鱼的感恩
  4. 允许Java App(applet)粘贴方法
  5. codeforces Gym 100338E Numbers (贪心,实现)
  6. python之路——目录
  7. javase(11)_juc并发库
  8. ajax实现上传图片保存到后台并读取
  9. 译文 编写一个loader
  10. 【git】不检查特定文件的更改情况