(组合数求模=乘法逆元+快速幂) Problem Makes Problem

题目:

As I am fond of making easier problems, I discovered a problem. Actually, the problem is ‘how can you make n by adding k non-negative integers?’ I think a small example will make things clear. Suppose n=4 and k=3. There are 15 solutions. They are

  1.  0 0 4
    
  2.  0 1 3
    
  3.  0 2 2
    
  4.  0 3 1
    
  5.  0 4 0
    
  6.  1 0 3
    
  7.  1 1 2
    
  8.  1 2 1
    
  9.  1 3 0
    
  10. 2 0 2

  11. 2 1 1

  12. 2 2 0

  13. 3 0 1

  14. 3 1 0

  15. 4 0 0

As I have already told you that I use to make problems easier, so, you don’t have to find the actual result. You should report the result modulo 1000,000,007.

Input
Input starts with an integer T (≤ 25000), denoting the number of test cases.

Each case contains two integer n (0 ≤ n ≤ 106) and k (1 ≤ k ≤ 106).

Output
For each case, print the case number and the result modulo 1000000007.

Sample Input
4

4 3

3 5

1000 3

1000 5

Sample Output
Case 1: 15

Case 2: 35

Case 3: 501501

Case 4: 84793457

分析与解答

b * k ≡ 1 (mod p) 是什么意思?
就是(b*k)%p=1
a mod b是什么意思?
就是a%b
这两个所有博客没人说,但是我不清楚。。

先看看什么是乘法逆元

当我们要求(a / b) mod p的值,且 a 很大,无法直接求得a / b的值时,我们就要用到乘法逆元。

满足 b * k ≡ 1 (mod p) 的 k 的值就是 b 关于 p 的乘法逆元。

我们可以通过求 b 关于 p 的乘法逆元 k,将 a 乘上 k 再模 p,即 (a * k) mod p。其结果与(a / b) mod p等价。

证:
因为 b * k ≡ 1 (mod p)
则有 b * k = p* x+1
得到 k = (p * x + 1) / b
将 k 代入(a * k) mod p
得到:
(a * (p * x + 1) / b) mod p
=((a * p * x) / b + a / b) mod p
=[((a * p * x) / b) mod p +(a / b)] mod p
=[(p * (a * x) / b) mod p +(a / b)] mod p
=(0 + (a / b)) mod p
= (a/b) mod p

1.用欧几里得扩展求逆元

ax≡1(modp)
可以等价的转化为ax−py=1 ,
检查gcd(a,p)是否等于1 ,如果是的话
套用exgcd解方程
最后解出x即可
求出来的x有可能为负数,所以结果进行变化:
x = (x * (c/gcd) % b + b) % b; 即的a的乘法逆元的解x

void Euild(ll a, ll b, ll &x, ll &y)  
{if(0 == b){x = 1, y = 0;return ;}Euild(b, a%b, x, y);ll flag = x;x = y;y = flag - a/b * y;
}

2.用费马小定理
费马小定理:假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p)
所以a*a^(p-2)≡1(mod p)
那a^(p-2)就是a的乘法逆元
可以利用快速幂计算
2.1那看看什么是快速幂
这里写图片描述

3^7 = 3 * 3^2 * 3^4
(7)10 = (111)2
———–4 2 1
3^15 = 3 * 3^2 * 3^4 * 3^8
(15)10 = (1111)2
———— 8 4 2 1
3^5 = 3 * 3^2 * 3^4
(5)10 = (101)2
———- 4 2 1

快速幂求x的y次方代码:

int pow_1(int x,int y){//x的y次方 int ren=x;int ans=1;while(y){if(y&1) ans*=ren;//取当前最末位的y,如果是1就继续乘ren ren*=ren;//下一位ren是当前ren的平方 1 2 4 8,这里8是x^4的平方,不是4的平方 y=y>>1;//y前进一位 }return ans;
} 

3.用递推法On求解

O(n)求1~n逆元表
有时会遇到这样一种问题,
在模质数p下,求1~n逆元 n< p

这个问题有种很牛的算法,其基于以下的推导:
在求i的逆元时
p%i+[p/i]i=p
令a=p%i,b=[p/i],则有
a+b
i=p
a+bi=0(mod p)
b
i=-a(mod p)
i^-1=-b/a
也就是说i的逆元为:-[p/i]*(p%i)^-1
而p%i<i,那么可以从2递推到n求逆元,在求i之前p%i一定已经求出
这样就可以O(n)求出所有逆元了
(初始化 1^(-1)=1)
代码如下

inv[1] = 1;  
for (int i = 2; i<MAXN; i++)  inv[i]=(long long)(p-p/i)*inv[p%i]%p;

好了现在我们再看这题

代码参考:

这题一个数拆成m个数相加,拆成的数可以是0,问有这m个数一共有几种情况,看成是n个小球放到m个盒子里。
比如四个球放到三个盒子,盒子可以为空,怎么算的我实在不懂,先记住公式吧 方案数就是:C(n+m-1,m-1)
组合数公式:
约定f(a)代表a的阶乘, C(m,n) = f(m)/(f(n)*f(m-n));
在本题就是C(n+m-1,m-1) = f(n+m-1)/(f(m-1)f(n));
本题代码实现:
我们打表用数组存一个数的阶乘,
f(m)/(f(n) * f(m-n))%mod=(f(m)/f(n))%mod * (1/f(m-n))%mod
=f(m) * quick(f(m-1),mod-2) % mod
quick(f(n),mod-2) % mod
a[n+k-1]quick(c,mod-2)%modquick(d,mod-2)%mod

#include <bits/stdc++.h>
#define mod 1000000007
#define ll long long
using namespace std;
ll a[2000000];
void C()//阶乘打表 
{memset(a,0,sizeof(a));a[0] = a[1] = 1 ;for(int i = 2 ; i <=2000000;i++)a[i] = a[i-1]*i%mod;
}
ll quick(ll a , ll b)//快速幂取模 
{ll res = 1 ;while(b){if(b&1) res = res *  a %mod ;b>>=1;a = a * a %mod ;}return res ;
}
int main()
{int t ;cin>>t;C();for(int cas = 1 ; cas<=t;cas++){ll n , k ;cin>>n>>k;ll c = a[k-1];ll d = a[n];printf("Case %d: ",cas);cout<<a[n+k-1]*quick(c,mod-2)%mod*quick(d,mod-2)%mod<<endl;}return 0;
}

现在做一下推广

我们组合数求模的板子

/*
mod=1e6+3
样例输入方式:
3
4 2
5 0
6 4
*/
//这里直接求出C(M,N)%mod#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<iostream>  
#include<cmath>  using namespace std;  
const int INF=0x3f3f3f3f;  
typedef long long LL;  
const int mod=1e6+3;  
const int maxn=1e6+100;  
LL fac[maxn],inv[maxn];  
LL Pow(LL a,LL b)  
{  LL ans=1;  while(b)  {  if(b&1)  ans=(ans*a)%mod;  a=(a*a)%mod;  b>>=1;  }  return ans;  
}  
int main()  
{  int cas=0;  int n,a,b;  fac[0]=1; //inv[0]=for(int i=1;i<=1000000;i++)  {  fac[i]=(fac[i-1]*i)%mod; //对阶乘打表 //   inv[i]=Pow(fac[i],mod-2);  }  scanf("%d",&n);  while(n--)  {  scanf("%d%d",&a,&b);  long long c=Pow(fac[b],mod-2); long long d=Pow(fac[a-b],mod-2); LL ans=fac[a]*c%mod*d%mod;  printf("Case %d: %lld\n",++cas,ans);  }  return 0;  
}  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/560679.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php做图书网站,基于PHP的图书馆网站管理系统的设计与实现

《现代图书情报技术 》 年 第 期 工作交流 总第 期基于 的图书馆网站管理系统的设计与实现 郑婷婷 张 羽 辽宁大学图书馆 沈阳 【摘要 】 阐述了在 下 , 使用 十 设计并实现 了图书馆网站管理系统 。 分系统平台如何搭建 、 系统结构如何设计以及最终如何实现三大部分 。 【关键…

(小费马定理降幂)Sum

题目&#xff1a; 分析与解答&#xff1a; 参考思路&#xff1a; https://www.cnblogs.com/stepping/p/7144512.html https://blog.csdn.net/strangedbly/article/details/50996908 根据隔板定理&#xff0c;把N分成一份的分法数为C(1,n-1)&#xff0c; 把N分成两份的分法…

温度 数值模拟 matlab,西安交通大学——温度场数值模拟(matlab)

西安交通大学材料制备与成型实验——温度场数值模拟,matlab编程温度场模拟matlab代码&#xff1a;clear,clc,clfL18;L28;N9;M9;% 边长为8cm的正方形划分为8*8的格子 T0500;Tw100; % 初始和稳态温度 a0.05; % 导温系数tmax600;dt0.2; % 时间限10min和时间步长0.2s dxL1/(M-1);dy…

matlab 参数识别,[转载]自编最小二乘法的Matlab参数辨识程序(含实例)

function [sysd,sys,err] ID(Y,U,Ts)%%基于递推最小二乘法的参数辨识程序%仅针对二阶系统&#xff1a;)%出处&#xff1a;http://blog.sina.com.cn/xianfa110%---------------%Inputs:%---------------%Y nX1 vector of your model output%U nX1 vector of your model input…

(回文串全排列个数) xiaoxin juju needs help

题目 As we all known, xiaoxin is a brilliant coder. He knew palindromic strings when he was only a six grade student at elementry school. This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gav…

让apache解析html里的php代码,让Apache解析html文件中的php语句

为什么要干这种事呢&#xff1f;原因在于:对于纯粹的网页来说(不涉及对于数据库的操作)&#xff0c;可以使用一些软件来生成html代码。推荐软件Axure但是&#xff0c;当生成html文件之后&#xff0c;你发现还要写php语句对数据库进行操作时&#xff0c;就会遇到一些问题。首先&…

(找循环节)Number Sequence

题目&#xff1a; A number sequence is defined as follows: f(1) 1, f(2) 1, f(n) (A * f(n - 1) B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case contains…

git获取管理员权限 windows,windows下git怎么管理权限

一、安装软件&#xff1a;msysGit-fullinstall-1.8.1.2打开之后设置安装路径&#xff0c;默认为C:\msysgit&#xff0c;如图&#xff1a;注意&#xff1a;如果要自定义安装路径&#xff0c;请不要安装在带有空格的路径以及含有中文的路径下点击“OK”以后开始安装&#xff0c;首…

(lucas) Saving Beans

题目&#xff1a; Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They suppose that…

java applet程序设计,Java Applet程序设计基础

Java Applet程序设计基础Java Applet 是用Java 语言编写的一些小应用程序&#xff0c;这些程序是直接嵌入到页面中&#xff0c;由支持Java的浏览器(IE 或 Nescape)解释执行能够产生特殊效果的程序。它可以大大提高Web页面的交互能力和动态执行能力。包含Applet的网页被称为Java…

(矩阵快速幂)解所有类似Fibonacci 的题目

Description In the Fibonacci integer sequence, F0 0, F1 1, and Fn Fn − 1 Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … An alternative formula for the Fibonacci sequence is Gi…

java ssm框架登录代码,求一个SSM框架登录功能的源码,要求能运行成功

[XML] 纯文本查看 复制代码<?xml version"1.0" encoding"UTF-8"?>xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xmlns:aop"http://www.springframework.org/schema/aop"xmlns:context"http://www.springframewo…

php getdbused,PHP之购物车

该文章记录了购物车的实现代码&#xff0c;仅供参考book_sc_fns.phpinclude_once(output_fns.php);include_once(book_fns.php);include_once(db_fns.php);include_once("user_auth_fns.php");include_once("admin_fns.php");include_once("data_vali…

java中spring的web支持nio,Spring WebClient NIO功能和问题域,与Spring Webflux一起使用

我正在使用最新版本的Spring - Spring 5 .我正在开发http客户端的WebService“聚合器”&#xff0c;有点像路由请求到外部WebServices&#xff0c;接收响应后接收响应&#xff0c;做一些数据操作并回复我的HTTP服务的客户端 .为了在我的应用程序中创建http客户端&#xff0c;我…

2018.9.15,Arduino—流水灯实验报告

实验任务和目的 通过Arduino控制LED形成流水灯效果 实验条件 Arduino UNO&#xff0c;面包板&#xff0c;6个LED&#xff0c;6个220Ω电阻 实验过程和结果 实验详细步骤&#xff1a; 在各LED正极和Arduino引脚之间串联一个限流电阻&#xff0c;并将LED负极与Arduion的GND相连 …

php封装redis类,php封装redis操作类

Redis系列-key相关主要操作函数_数学_自然科学_专业资料。这篇 blog 主要总结下,redis 中跟 key 相关的常用函数 1)keys 语法:keys pattern 解释:查找所有匹配指定......Redis 实现分析网易杭研——胡炜 OUTLINE ? REDIS的内部实现(基于2.8.7)––––––––– 单线程模型的…

liunx php apache2,linux apache2部署php

android-plugmgr源代码分析android-plugmgr是一个Android插件加载框架,它最大的特点就是对插件不需要进行任何约束.关于这个类库的介绍见作者博客,市面上也有一些插件加载框架,但是感觉没有这个好.在这篇文章中,我 ...JS延时提示框p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; fon…

2018.9.10.Matlab实验一:熟悉Matlab开发环境

一、实验任务和目的 1. 熟悉Matlab的系统环境及基本操作方法。 2. 掌握Matlab的搜索路径及其设置方法。 3. 熟悉Matlab的帮助信息的查阅方法。 二、实验内容 1. 熟悉 Matlab 工作界面的多个常用窗口的及使用方法。 熟悉Command windows、Workspace、Command history、C…

2018.9.10.Matlab实验二:Matlab基本运算

实验二&#xff1a;Matlab基本运算 一、实验任务和目的 1. 掌握变量的定义与数据类型。 2. 掌握变量的初始化方法。 3. 掌握数组、多维数组与子数组的定义、存储、赋值、变换。 4. 掌握逻辑数组的用法。 5. 熟悉MATLAB常用的函数、常用标点和快捷键。 二、实验内容 1. …