| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 471 人关注过本帖
标题:Walking on the Grid
只看楼主 加入收藏
欣飞飞
Rank: 1
等 级:新手上路
帖 子:20
专家分:1
注 册:2013-10-6
结帖率:100%
收藏
 问题点数:0 回复次数:1 
Walking on the Grid
Problem Description

Biving lives in Grid Kingdom, which is a special country as all its cities lie in a grid of size W*H.
Biving’s home locates in grid(1, 1) and she wants to go to grid(W, H) as soon as possible. In each step, she can walk from grid(I, J) to grid(I+1, J) or grid(I, J+1), but she can never walk out of the grid.
Here comes the question, how many path Biving can choose to achieve her goal. Two path Pi and Pj are treat as different if there exist some step Pi going to grid(x, y) but Pj don’t.

Input

The input contains multiple test cases (<= 100).
The first line of each test case contains two integer W, H(1<=W<=30, 1<=H<=30).

Output

For each case, output the path’s number modulo 1,000,000,007 in a single line.

Sample Input
1 1
2 2
3 3
Sample Output
1
2
6

怎么控制不让他超出范围?

#include<iostream>
using namespace std;
int main()
{
    int W,H;
    while(cin>>W>>H)
    {
        int sum1=1,sum2=1;
        for(int i=1;i<W-1;i++)
        {
            sum1*=i;
        }
        for(int j=W+H-2;j>=H;j--)
        {
            sum2*=j;
        }
        cout<<sum2/sum1<<endl;
    }
    return 0;
}
搜索更多相关主题的帖子: different possible question achieve country 
2013-11-10 19:45
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
自己写个长整数类,或者用double(当然不是 (a*b*c……)/(d*e*f……),而是a/d * b/e + c/f + ……)。
这里之所以可以用double,是因为对于公式 C(W+H-2,W-1) 而言,大部分是可约分的,也就是说精度上不会导致过大的误差
2013-11-11 08:59
快速回复:Walking on the Grid
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024488 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved