Codeforce 57C Array
2 seconds
256 megabytes
standard input
standard output
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of n, containing only integers from 1 to n. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
- each elements, starting from the second one, is no more than the preceding one
- each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
The single line contains an integer n which is the size of the array (1 ≤ n ≤ 105).
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
2
4
3
17
/***
题意:有一个长度为n的数字串,其中的数字都是1~n的数字,问:有多少种串能够满足下列两种情况:
非降序列;
非升序列。
两者等价,可以求出一种,然后另外一种就求出来了。 高中就能做出来,现在反而不会了= =!
借用模型,求出组合数学的公式,用数论的方法进行求解,如此即可。难点是数论上的方法求解。 先说模型:
高为1 ,长为2n-1的矩形,由1*1的小方格排列而成,然后,我们选取其中n-1个小方格,放上空格标志,
在剩下没有标志的空格里,记录从第一个小方格到当前小方格中,有多少个空格标志,将数量记录在方格中,
如此,满足非降序列条件的序列便可以得出,只不过,数字的范围是从0 ~ n-1,与从1 ~ n是等价的。 这种序列可以得到C(2n-1,n)个,同样的,满足非升序列的种数也是C(2n-1,n),
但是,两种序列中有重复的:序列为0 0 0 0 0 0 …… 0,~ ,n-1,n-1,n-1,n-1,n-1,n-1,……n-1,
共有n中,
因此,
最终答案是,2C(2n-1,n)-n C(2n-1,n)mod p的求解: ((2n-1)!/n!*n!)mod p
先求n!*n!关于p的逆元m,
=((2n-1)!mod p)*(m mod p) mod p 而求n!*n!的逆元,可以根据费马小定理:a^(p-1) mod p = 1 (mod p),成立的充分必要条件是a与p互质,
因此,当p为质数时(充分条件),等式仍成立。 逆元m满足 (n!*n!*m)mod p = 1恒成立,
已知的 p=1000000007,是质数,因此,可以根据费马小定理得到(n!*n!)*(n!*n!)^(p-2) mod p = 1,
因此,m= (n!*n!)^(p-2).
***/ #include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <vector>
#include <bitset>
#include <cstdio>
#include <string>
#include <numeric>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional> using namespace std;
typedef long long ll;
typedef unsigned long long ull; int dx[4]= {-1,1,0,0};
int dy[4]= {0,0,-1,1}; //up down left right
bool inmap(int x,int y,int n,int m)
{
if(x<1||x>n||y<1||y>m)return false;
return true;
}
int hashmap(int x,int y,int m)
{
return (x-1)*m+y;
} #define eps 1e-8
#define inf 0x7fffffff
#define debug puts("BUG")
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define lowbit(x) (-x)&x #define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
#define mod 1000000007 int n;
ll res,denom; ll power(ll x,ll y)
{
ll ans = 1;
while(y)
{
if(y&1)
ans = ans * x %mod;
x = x*x%mod;
y>>=1;
}
return ans;
} int main()
{
cin>>n;
res = 1;
denom=1;
for(int i=2*n; i>n; i--)//2n*……*n+1
res = res * i%mod;
for(int i=1; i<=n; i++)//n!
denom = denom*i%mod;
denom = power(denom,(ll)mod-2);//变除为乘,求逆元
cout<<(res*denom-n+mod)%mod<<endl;
return 0;
}
Codeforce 57C Array的更多相关文章
- Codeforces 57C Array dp暴力找到规律
主题链接:点击打开链接 的非增量程序首先,计算, 如果不增加的节目数量x, 非减少一些方案是x 答案就是 2*x - n 仅仅需求得x就可以. 能够先写个n3的dp,然后发现规律是 C(n-1, 2* ...
- CodeForces 57C Array 组合计数+逆元
题目链接: http://codeforces.com/problemset/problem/57/C 题意: 给你一个数n,表示有n个数的序列,每个数范围为[1,n],叫你求所有非降和非升序列的个数 ...
- CodeForce 439C Devu and Partitioning of the Array(模拟)
Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabytes ...
- Codeforce 1175 D. Array Splitting
新鲜热乎的题 Codeforce 1175 D. 题意:给出一个长度为$n$的序列$a$,你需要把它划分为$k$段,每一个元素都需要刚好在其中一段中.分好之后,要计算$\sum_{i=1}^{n} ( ...
- Codeforce 1155D Beautiful Array(DP)
D. Beautiful Array You are given an array aa consisting of nn integers. Beauty of array is the maxim ...
- codeforce 121E - Lucky Array
10^4以内只由4和7构成的数字只有31种,那么做法就很简单了,求出每个数字与其最接近的幸运数的差值,然后建立线段树,线段树维护区间最小值和最小值个数,如果操作过程中最小值<0,那么就去对差值进 ...
- 三维dp&codeforce 369_2_C
三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...
- CodeForce 359C Prime Number
Prime Number CodeForces - 359C Simon has a prime number x and an array of non-negative integers a1, ...
- javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈
Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...
随机推荐
- N使用exus2打造企业maven仓库(三)
假设项目中,我没有使用maven,我应该做出选择,或为项目.或者用它来推动这个项目从maven.有人会问,为什么maven?无需maven我们没有很好的操作. 这里,只说两件事情我最欣赏:第一点是管理 ...
- [Cocos2d-x开发问题-3] cocos2dx动画Animation介绍
Cocos2d-x为了减少开发难度,对于动画的实现採用的帧动画的方案.这也就是说Cocos2d-x中的动画是帧动画. 帧动画的原理相信大家都不陌生,就是多张图片循环播放以实现动画的效果. 一个简单的动 ...
- easyhadoop:failed to open stream:Permission denied in /var/www/html/index.php
今天又重新部署了下easyhadoop,结果apache后台服务器报这个错误: [Fri Dec 13 10:32:41 2013] [notice] SIGHUP received. Attempt ...
- linux下编译qt5.6.0静态库——configure配置(超详细,有每一个模块的说明)(乌合之众)
linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...
- Qt递归拷贝和删除目录
最近在翻看项目代码时,看到了这两个函数,想到这个功能十分常用,因此拿出来与大家分享,希望对大家有用.几点说明: 1.记得当初写代码那会,是参考了网上的帖子写的,做了一点小修改.因此代码源于网络. 2. ...
- MYSQL 语法大全自己总结的
mysql语法大全 --------数据链接---------------------数据库服务启动net start mysql --关闭服务net stop mysql --登录 -u,-p后面不 ...
- SIMPASS技术解析
一.什么叫SIMPASS SIMpass技术融合了DI卡技术和SIM卡技术,或者称为双界面SIM卡.SIMpass是一种多功能的SIM卡,支持接触与非接触两个工作接口,接触界面实现SIM功能,非接触界 ...
- BCM wifi驱动学习
BCMwifi驱动学习 一.wifi详解1 1.代码路径:Z:\home\stonechen\svn\TD550_X\TD550\3rdparty\wifi\BCM43362\special\bcmd ...
- Java描述语言、国家和地理的类——Locale
Locale类代表一个特定的地理.语言和国家环境.一个Locale的实例对象本身不会验证它代表的语言和国家地区信息是否正确,只是向一些对国家和语言.地理等比较敏感的类提供国家地区语言信息,这些类有Da ...
- 浅谈MySQL 数据库性能优化
MySQL数据库是 IO 密集型的程序,和其他数据库一样,主要功能就是数据的持久化以及数据的管理工作.本文侧重通过优化MySQL 数据库缓存参数如查询缓存,表缓存,日志缓存,索引缓存,innodb缓存 ...