zoukankan      html  css  js  c++  java
  • hdu5698瞬间移动(杨辉三角+快速幂+逆元)

    瞬间移动

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 2121    Accepted Submission(s): 949


    Problem Description
    有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第n行第m列的格子有几种方案,答案对1000000007取模。

     
    Input
    多组测试数据。

    两个整数n,m(2n,m100000)
     
    Output
    一个整数表示答案
     
    Sample Input
    4 5
     
    Sample Output
    10

    题意:从左上角开始走,每次只能走到右下任意位置,找规律后发现他是一个斜着的杨辉三角。引用一下别人的

    题目中的n,m是第n行第m列,所以可以得到组合数c(m+n-4,m-2)这个就是答案,用快速幂加逆元计算结果

     1 #include <iostream>
     2 using namespace std;
     3 typedef long long ll;
     4 const int mod = 1e9 + 7;
     5 ll quick_pow(ll a, ll b) {      ///快速幂求逆元
     6     ll ans = 1;
     7     while(b) {
     8         if(b % 2) {
     9             ans *= a;
    10             ans %= mod;
    11         }
    12         a *= a;
    13         a %= mod;
    14         b /= 2;
    15     }
    16     return ans;
    17 }
    18 ll C(int m, int n) {         ///组合数公式:C(m, n) =m!/(n!(m-n)!)
    19     if(n > m)
    20         return 0;
    21     ll ans = 1;
    22     for(int i = 1; i <= n; i++) {
    23         ll a, b;
    24         a = (m - n + i) % mod;
    25         b = i % mod;
    26         ans = ans * (a * quick_pow(b, mod - 2) % mod) % mod;
    27     }
    28     return ans;
    29 }
    30 int main() {
    31     ll m, n;
    32     while(~scanf("%lld %lld",&n,&m)) {
    33         printf("%lld
    ",C(m+n-4,m-2));
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    微信商城中使用微信支付接口获取用户地址
    微信支付开发流程
    沉默多年,重新开博
    Extjs 表单验证后,几种错误信息展示方式
    自己对Extjs的Xtemplate的忽略
    js execCommand
    支付宝支付
    C# 将短时间格式变长正常时间格式
    SortedDictionary
    sql操作
  • 原文地址:https://www.cnblogs.com/fqfzs/p/9860848.html
Copyright © 2011-2022 走看看