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)
然后做完了
#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
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/Other ...
- HDU5924 Mr. Frog’s Problem
/* HDU5924 Mr. Frog’s Problem http://acm.hdu.edu.cn/showproblem.php?pid=5924 数论 * */ #include <cs ...
- SPOJ Another Longest Increasing Subsequence Problem 三维最长链
SPOJ Another Longest Increasing Subsequence Problem 传送门:https://www.spoj.com/problems/LIS2/en/ 题意: 给 ...
- 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 ...
- hdu5924Mr. Frog’s Problem
Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- SPOJ - LOCKER
SPOJ - LOCKERhttps://vjudge.net/problem/45908/origin暴力枚举2-102 23 34 2 25 2 36 3 37 2 2 38 2 3 39 3 3 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 【无源汇上下界最大流】SGU 194 Reactor Cooling
题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=194 题目大意: n个点(n<20000!!!不是200!!!RE了无数次) ...
- 10.6 CCPC northeast
1001 Minimum's Revenge 点的编号从 1 到 n ,u v 的边权是 LCM(u,v) ,求这个图的最下生成树 搞成一颗以 1 为 根 的菊花树 ---------------- ...
随机推荐
- P2421 A-B数对(增强版)
题目背景 woshiren在洛谷刷题,感觉第一题:求两数的和(A+B Problem)太无聊了,于是增加了一题:A-B Problem,难倒了一群小朋友,哈哈. 题目描述 给出N 个从小到大排好序的整 ...
- numpy.random.randint
low.high.size三个参数.默认high是None,如果只有low,那范围就是[0,low).如果有high,范围就是[low,high). >>> np.random.ra ...
- hash 散列表
一个字符串的hash值: •现在我们希望找到一个hash函数,使得每一个字符串都能够映射到一个整数上 •比如hash[i]=(hash[i-1]*p+idx(s[i]))%mod •字符串:abc,b ...
- 公共Service的抽取小例
package cn.sxx.service; import java.util.List; public interface BaseService<T,Q> { public void ...
- bootstrap 翻页(对齐的链接)
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- [LUOGU] P2543 [AHOI2004]奇怪的字符串
LCS //Writer:GhostCai && His Yellow Duck #include<iostream> #include<cstring> #d ...
- mysqldump导出备份数据库报Table ‘performance_schema.session_variables‘ doesn‘t exist
今天在bash进行本地数据库往云端数据库导数据的时候,在本地导出.sql文件这第一步就出现了错误问题,导出sql文件的命令: mysqldump -u 用户名 -p 数据库名 > xxx.sql ...
- (39)zabbix snmp自定义OID nginx监控实例
为什么要自定义OID? 前面的文章已经讲过zabbix如何使用snmp监控服务器,但是他有一个很明显的局限性:只能监控定义好的OID项目 假如我们想知道nginx进程是否在运行?在没有zabbix a ...
- 如何用纯 CSS 创作在文本前后穿梭的边框
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qYepNv 可交互视频教 ...
- python--线程的其他方法
一 . current_thread的用法 import threading import time from threading import Thread, current_thread def ...