Greedy walking spoj-ucv2013E (乘法逆元,数论基础)
来源:互联网 发布:邵氏孤儿 知乎 编辑:程序博客网 时间:2024/06/05 08:02
Reginald is an N-dimensional traveler who wants to return to Filipistonia’s Kingdom. He has an Obsessive-compulsive disorder in the way he travels so he can only do it following particular rules:
Every step is exactly one unit long.
He only moves in one dimension at a time.
He only travels along the positive direction for each dimension.
For example, when traveling on a two-dimensional place. He can travel along either the X or the Y axis at any given time, but never on both at the same time. Moreover, since he only travels along the positive direction and every step is one unit long, his only possible moves are (+1, 0) and (0, +1).
Greedy Walking example
As you can see, he is a Greedy Walker: once he makes a decision he assumes it is the correct and he never goes back.
Given a starting position in an N-dimensional space (x1i, x2i, … ,xni) your task is to count the number of different travels he can make to position (x1f, x2f, … , xnf) modulo 1000000007.
Input
The input contains several test cases, each one corresponding to a single travel. Each test case consists of a single line with one integer (1 <= N <= 50) followed by two lines each one with N integers, fi rst line will be initial position and second line will be target position.
You can assume that 0 <= xki <= x1f <= 500 for all k, 1 <= k <= N and Sum(xki-xkf) <= 500.
The end of input is indicated by a test case with N = 0.
Output
For each travel output a single line with one integer, the number of different travels that exist from the initial position to the final position modulo 1000000007.
Example
Input:
2
2 1
5 5
4
0 0 0 0
1 2 3 4
5
1 2 3 4 5
8 5 6 4 8
5
0 0 0 0 0
100 100 100 100 100
0
Output:
35
12600
19219200
257055440
大神说得很清楚了
题意:你有一个n维空间,你要从起点走向终点且只能向正方向走,问,有多少种走法可以走到终点。
解题思路:因为只能向正方向走,所以总共走的步数必定为各个轴上起点和终点的差的总和,那么设要走的步数为sum,对于一个轴而言,如果他们起点和终点在这个轴上的差为a,那么,我们只要在这sum步中选择任意a步走向这个轴的正方向即可,所以他的走法为C(sum,a),那么根据这个思路,我们再在剩下的步数中按照这个方法依次递推即可。例如第二个样例答案即为C(10,4)*C(6,3)*C(3,2)*C(1,1)=10!/(4!*3!*2!*1!)=12600
因为所求的组合数过大,所以我们要用逆元的方法求答案。
重点! 一个取模过的数除去一个数 或者除一个很大的数 相当于乘上除数的逆元。 一堆数的乘法逆元可以由最大的数的乘法逆元推出。求一个数的乘法逆元可以快速幂 (数,mod-2),还有 mod必须是质数。 乘法逆元的一个性质
http://blog.csdn.net/enamor_AC/article/details/56284851
#include <cstdio>#define mod 1000000007#define maxn 505typedef long long ll;ll fac[maxn],inv[maxn],n,a[maxn],b[maxn],sum;ll quickpower(ll a,ll b){ ll ans=1; while(b){ if(b&1){ ans*=a; ans%=mod; } a=(a*a)%mod; b=b>>1; } return ans;}void init(){ fac[0]=1; for(ll i=1;i<maxn;i++) fac[i]=fac[i-1]*i%mod; inv[maxn-1]=quickpower(fac[maxn-1],mod-2); //求最大的数的逆元 for(ll i=maxn-2;i>=0;i--) inv[i]=inv[i + 1]*(i + 1)%mod; //其他的数的逆元 可以根据最大的数往回推 inv[i]=inv[i+1]*(i+1)%mod;}int main(){ ll n,ans; init(); while(scanf("%lld",&n)&&n){ sum=0; for(ll i=0;i<n;i++) scanf("%lld",&a[i]); for(ll i=0;i<n;i++){ scanf("%lld",&b[i]); a[i]=b[i]-a[i]; sum+=a[i]; } ans=fac[sum]; for(ll i=0;i<n;i++){ ans=ans*inv[a[i]]%mod; } printf("%lld\n",ans); } return 0;}
- Greedy walking spoj-ucv2013E (乘法逆元,数论基础)
- [SPOJ UCV2013E Greedy Walking]数学、统计
- 【数论】乘法逆元
- 数论之乘法逆元
- 【数论】乘法逆元总结
- 乘法逆元数论篇
- SPOJ UCV2013E 数学
- SPOJ 15433 UCV2013E
- (ACM数论)求乘法逆元的各种姿势~
- 【数论】关于乘法逆元的证明
- 数论学习之乘法逆元
- 『数论』乘法逆元
- ZOJ3609——数论基础 扩展欧几里得求解乘法逆元
- 浅谈数论(四)扩展欧几里得算法与乘法逆元
- 关于数论乘法逆元及相关知识点
- 乘法逆元数论篇【ORZ式教学】
- 【数论】 欧几里德 扩展欧几里德 乘法逆元 挑战程序P115
- 51nod 1256 (最基础)乘法逆元
- 树链剖分 详解 比较好
- 算法作业HW13:Leetcode96 Unique Binary Search Trees
- 第一天
- hdu 3966 树链剖分 模板题
- MarkDown 图片大小问题
- Greedy walking spoj-ucv2013E (乘法逆元,数论基础)
- Vue通过ref父子组件拿值
- 做一个简单的射靶游戏
- Codeforces Round #415 (Div. 2) C. Do you want a date? 数学
- 大叔的高效团队定义,非常赞
- 详解coredump
- 思考一
- angular2笔记
- 算法--排序