题目:1119 机器人走方格 V2

思路:求C(m+n-2,n-1) % 10^9 +7       (2<=m,n<= 1000000)

   在求组合数时,一般都通过双重for循环c[i][j] = c[i-1][j] + c[i-1][j-1]直接得到。

   但是m,n都很大时,就会超时。

   

   利用公式:C(n,r) = n! / r! *(n-r)!  与  a/b = x(mod M)  ->  a * (b ^ (M-2)) =x (mod M)     进行求解

费马小定理:对于素数 M 任意不是 M 的倍数的 b,都有:b ^ (M-1) = 1 (mod M)

a/b = x(mod M)  ->  a * (b ^ (M-2)) =x (mod M)的推导:

  只要 M 是一个素数,而且 b 不是 M 的倍数,就可以用一个逆元整数 b’,通过 a / b = a * b' (mod M),来以乘换除。

  a/b = x(mod M)

  a / b = a / b * (b ^ (M-1)) = a * (b ^ (M-2)) = x(mod M)

  而b ^ (M-2) mod M 就是逆元整数 b`。

所以最终要求的 x = n! *[r! *(n-r)!]^(M-2)  (mod M)  

#include <cstdio>
#include <string> const int mod = ;
const int maxN = 1e6;
long long c[maxN* +];
int m,n; void init(){
c[] = ;
c[] = ;
for(int i =; i <= maxN*+; i++)
c[i+] = (c[i] *(i+) ) % mod;
}   
int main(){
init();
while(~scanf("%d%d",&n,&m))
{
long long ans = c[n - + m - ];
ans = (ans * pow(c[n-],mod - )) % mod;
ans = (ans * pow(c[m - ] ,mod - )) % mod;
printf("%lld\n",ans);
}
return ;
}

题目:1013 3的幂的和

思路:用公式求 等比数列 % 10^9+7

   这仍旧是除法取模;

sn=(a1(q^n-1))/(q-1) % M =  (a1(q^n-1))*(q-1)^ (M  -1) % M;
  
#include <iostream>

using namespace std;

const int mod = 1e9+;

long long pow(long long n,long long m)
{
long long ans = ;
while(m > )
{
if(m & )ans = (ans * n) % mod;
m = m >> ;
n = (n * n) % mod;
}
return ans;
} int main()
{
int n;
cin >>n;
cout<< ((pow(, n+)-)*pow(, mod-))%mod<<endl;
return ;
}
  

除法取模练习(51nod 1119 & 1013 )的更多相关文章

  1. 51nod 1013 3的幂的和 - 快速幂&除法取模

    题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013 Konwledge Point: 快速幂:https:/ ...

  2. 51nod1119(除法取模)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 题意:中文题诶- 思路:这题数据比较大直接暴力肯定是不 ...

  3. 51nod1119(除法取模/费马小定理求组合数)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 题意:中文题诶- 思路:这题数据比较大直接暴力肯定是不 ...

  4. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  5. Re.多项式除法/取模

    前言 emmm又是暂无 前置 多项式求逆 多项式除法/取模目的 还是跟之前一样顾名思义] 给定一个多项式F(x),请求出多项式Q(x)和R(x),满足F(x)=Q(x)∗G(x)+R(x),R项数小于 ...

  6. hdu 3037 费马小定理+逆元除法取模+Lucas定理

    组合数学推推推最后,推得要求C(n+m,m)%p 其中n,m小于10^9,p小于1^5 用Lucas定理求(Lucas定理求nm较大时的组合数) 因为p数据较小可以直接阶乘打表求逆元 求逆元时,由费马 ...

  7. 快速幂取模模板 && 51nod 1013 3的幂的和

    #include <iostream> #include <cstdio> #include <cmath> #include <vector> #in ...

  8. HDU 4633 Who's Aunt Zhang ★(Polya定理 + 除法取模)

    题意 用K个颜色给魔方染色,魔方只能整体旋转并且旋转重合的方案算一种,求一共有多少不同的染色方案. 思路 经典的Polya应用,记住正六面体的置换群就可以了,魔方就是每个大面变成9个小面了而已: 本题 ...

  9. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

随机推荐

  1. 浅析敏感词过滤算法(C++)

    为了提高查找效率,这里将敏感词用树形结构存储,每个节点有一个map成员,其映射关系为一个string对应一个TreeNode. STL::map是按照operator<比较判断元素是否相同,以及 ...

  2. xshell xftp

    xshell : http://www.netsarang.com/xshell_download.html xftp:http://www.netsarang.com/products/xfp_ov ...

  3. 双系统下(Ubuntu + win7)windows 无法连接无线网络

    双系统下(Ubuntu + win7)windows 无法连接无线网络 今天开机登录win7,突然发现无法使用无线网络(WiFi信号标志有个大红叉),于是查看设备驱动,一切正常,这就奇怪了:用Wind ...

  4. tunning-Instruments and Flame Graphs

    On mac os, programs may need Instruments to tuning, and when you face too many probe messages, you'l ...

  5. 阅读笔记Multi-task Learning for Stock Selection [NIPS1996]

    Multi-task Learning for Stock Selection  Joumana Ghosn and Yoshua Bengio 摘要 用人工神经网络预测未来回报以便于做出对应的金融决 ...

  6. 2.jenkins配置邮件提醒

    1.前言 在Jenkins的使用中邮件提醒是一个常用功能,Jenkins默认安装了Mailer Plugin插件用于实现此功能. 2.邮件服务器配置 首先在Jenkins的"系统管理&quo ...

  7. List集合基于某个字段排序

    using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Product { publ ...

  8. Nginx+tomcat负载均衡时静态页面报404

    百度到的问题解决BLOG http://os.51cto.com/art/201204/326843.htm nginx+2台tomcat负载均衡,应用程序已部署,单独访问tomcat时,可以访问到所 ...

  9. HTTP Client工具类

    HTTP Client工具类: import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.ht ...

  10. 利用windows服务+timer或者windows任务计划程序+控制台进行进行每日邮件推送

    1.邮件发送代码 using System.Text; using System.Net; using System.Net.Mail; using System.Reflection; using ...