(大组合数)D. Fence Building ------ ACM-ICPC 2017 Asia Urumqi
标签: 大组合数
传送门: D.Fence Building
Farmer John owns a farm. He first builds a circle fence. Then, he will choose n points and build some straight fences connecting them. Next, he will feed a cow in each region so that cows cannot play with each other without breaking the fences. In order to feed more cows, he also wants to have as many regions as possible. However, he is busy building fences now, so he needs your help to determine what is the maximum number of cows he can feed if he chooses these n points properly.
Input
The first line contains an integer 1 \le T \le 1000001≤T≤100000, the number of test cases. For each test case, there is one line that contains an integer n. It is guaranteed that 1 \le T \le 10^51≤T≤105 and 1 \le n \le 10^{18}1≤n≤1018.
Output
For each test case, you should output a line ”Case #ii: ans” where ii is the test caseS number, starting from 11and ans is the remainder of the maximum number of cows farmer John can feed when divided by 10^9 + 7.
样例输入
3 1 3 5
样例输出
Case #1: 1 Case #2: 4 Case #3: 16
题目来源
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const long long int mod=1e9+7;
ll mod_pow(ll x, ll n, ll p){ //快速幂
ll res = 1;
while(n){
if(n & 1) res =res * x % p;
x = x * x % p;
n >>= 1;
}
return res;
}
ll comb(ll n, ll m, ll p){ //comb用来求解组合数
if(m > n) return 0;
ll ret = 1;
m = min(n - m, m);
for(int i = 1; i <= m; i ++){
ll a = (n + i - m) % p;
ll b = i % p;
ret = ret * (a * mod_pow(b, p - 2, p) % p) % p;
}
return ret;
}
ll Lucas(ll n, ll m, ll p){ //卢卡斯定理---处理大的组合数对素数取模的情况,因为这时如果递推的话将会特别耗时
if(m == 0) return 1;
return comb(n % p, m % p, p) * Lucas(n / p, m / p, p) % p;
}
int main(){
int T;
ll n, m, p;
scanf("%d", &T);
int cas=1;
while(T--){
scanf("%lld",&n);
long long ans=(Lucas(n,2,mod)+Lucas(n,4,mod)+1)%mod;
printf("Case #%d: %lld\n",cas++,ans);
}
return 0;
}
智能推荐
HDU-2017 ACM/ICPC Asia Regional Shenyang Online-1005-number number number
ACM模版 描述 题解 矩阵乘法,简单裸题。 代码...
HDU-2017 ACM/ICPC Asia Regional Shenyang Online-1001-string string string
ACM模版 描述 题解 后缀自动机……我知识的盲区,学过 AC 自动机,后缀数组,等等算法,就是没有学过这个……怪不得做不出来。 建立后缀自动机后,对每个节点统计出现次数,然后对字符串的后缀进行加一,更新每个节点的父节点就好了。 代码...
web安全简易规范123
web安全,大公司往往有专门的安全开发流程去保证,有专门的安全团队去维护,而对于中小网络公司,本身体量小,开发同时兼带运维工作,时间精力有限,但是,同样需要做一些力所能及的必要的事情。有时候,安全威胁并不是因为你的防盗窗被人撬开了,而是你晚上睡觉的时候忘了关门,而关上门对开发来说也许只是举手之劳。 1、不要用root,确定使用的中间件和框架是否默认打开了后门 我们总会在线上使用部署一些中间件、开源...
猜你喜欢
html5拖放--15行js代码实现两个div内容互换
本文首发于我的个人博客:http://cherryblog.site/ ,欢迎大家前去参观 本文项目地址,sortable插件地址:https://github.com/sunshine940326/sortable demo地址:https://github.com/sunshine940326/drag 在写我们后台的管理程序中需要有一个拖放的功能,然后我们有一个这样的功能,实现11个固定且大...
git切换分支报错,不管什么标题名字,都报非法字符,所以就不起名字了。
切换分支的时候,报了标题这么个错误,error: ”xxx did not match any file(s) known to git. 看见不能切换分支,我首先 git status 查看了一下当前状态,如下图 然后,就会发现,其实我的这个错误非常明显,就是在我的 beat 分支下有文件修改,所以切换不了。ok,解决方法: 1. 如果修改的这些文件没什么用,完全可以删除。(我这儿的...
Oracle分析函数之LEAD和LAG实际应用
Oracle分析函数之LEAD和LAG实际应用 在前几天的工作中按照客户的需求,需要对客户信息进行数据分析,即某人存在多个状态的账号,将客户信息账号状态分析出结果,和客户确认汇报,根据保留规则,保留唯一账号,以保证程序可用性。起初,根据聚合函数进行查询分析,需要写一大串的SQL,即不美观又复杂,很容易产生错误。后续想到Oracle分析函数中的lead和lag,SQL简洁了很多且容易产生报告数据。 ...
小知识积累(不断更新中)
判断变量的类型(数组,对象) tyopof:不推荐,因为无法区别数组与对象,数组是对象的子对象 instanceof:可以使用 还可以用来判断是否属于函数 Object.prototype.toString.call():最兼容,推荐使用 定时器的执行顺序或机制 js是单线程的,浏览器遇到setTimeout或者setInterval会把定时器推入浏览器的待执行事件队列里面但是不执行,先执行完当前...

