题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1067

1067 - Combinations

Given n different objects, you want to take k of them. How many ways to can do it?

For example, say there are 4 items; you want to take 2 of them. So, you can do it 6 ways.

Take 1, 2

Take 1, 3

Take 1, 4

Take 2, 3

Take 2, 4

Take 3, 4

Input

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

Each test case contains two integers n (1 ≤ n ≤ 106), k (0 ≤ k ≤ n).

Output

For each case, output the case number and the desired value. Since the result can be very large, you have to print the result modulo 1000003.

Sample Input

Output for Sample Input

3

4 2

5 0

6 4

Case 1: 6

Case 2: 1

Case 3: 15

分析:

时间只有2秒,T组测试数据加上n的106达到了109递推肯定超时,那么考虑组合公式,C(n,k)=n!/(k!*(n-k)!);先打一个阶乘的表(当然要取模,只有106),然后就是这个除法取模的问题,当然是求逆元,(a/b)%mod=a*(b对mod 的逆元);求逆元可以用扩欧和费马小定理。

费马小定理的使用条件mod必须为素数。

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define N 1000009
#define mod 1000003
#define LL long long

LL fact[N];

void init()
{
fact[0] = fact[1] = 1;

for(int i = 2; i < N; i++)
fact[i] = (fact[i-1] * i) % mod;
}
/*LL niyuan(int a, int p)
{
LL ans = 1;

if(p == 0)
return 1;

while(p)
{
if(p & 1)
ans = (ans * a) % mod;
a = (a * a) % mod;
p >>= 1;
}
return ans;

}*/

LL niyuan(int a, int b)///就是一个快速幂。
{
if(b == 0)
return 1;

LL x = niyuan(a, b / 2);

LL ans = x * x % mod;

if(b % 2 == 1)
ans = ans * a % mod;

return ans;
}
LL c(int n, int k)
{
LL fm = (fact[k] * fact[n-k]) % mod;///n! * (n-m)!.
LL ans1 = niyuan(fm, mod - 2);///求n!的逆元。

return (ans1 * fact[n]) % mod;///公式(a / b ) % mod = (a * a ^(mod-2) % mod。
}
int main(void)
{
int T, cas;
int n, k;

init();
scanf("%d", &T);
cas = 0;

while(T--)
{
cas++;
scanf("%d%d", &n, &k);
if(2 * k > n)///组合数性质。
k = n - k;

printf("Case %d: %lld\n", cas, c(n, k));

}
return 0;
}

light oj 1067 费马小定理求逆元的更多相关文章

  1. LightOJ 1419 – Necklace Polya计数+费马小定理求逆元

    题意:给你n个珠子可以染成k种颜色,旋转后相同的视为一种,问共有几种情况 思路:开始按照一般的排列组合做发现情况太多且要太多运算,查了下发现此题是组合中Polya定理模板题- 学的浅只能大致一说公式S ...

  2. 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】

    链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...

  3. UVALive-3722 留个坑,为什么费马小定理求逆元不对??

    #include <iostream> #include <cstdlib> #include <queue> #include <algorithm> ...

  4. hihocoder #1698 假期计划 (排列组合+费马小定理+乘法逆元)

    Description 小Ho未来有一个为期N天的假期,他计划在假期中看A部电影,刷B道编程题.为了劳逸结合,他决定先拿出若干天看电影,再拿出若干天刷题,最后再留若干天看电影.(若干代指大于0)  每 ...

  5. 洛谷 - P1593 - 因子和 - 费马小定理

    类似的因为模数比较小的坑还有卢卡斯定理那道,也是有时候逆元会不存在,因为整除了.使用一些其他方法避免通过逆元. https://www.luogu.org/fe/problem/P1593 有坑.一定 ...

  6. 51nod A 魔法部落(逆元费马小定理)

    A 魔法部落 小Biu所在的部落是一个魔法部落,部落中一共有n+1个人,小Biu是魔法部落中最菜的,所以他的魔力值为1,魔法部落中n个人的魔法值都不相同,第一个人的魔法值是小Biu的3倍,第二个人的魔 ...

  7. 题解 P4071 【[SDOI2016]排列计数】 (费马小定理求组合数 + 错排问题)

    luogu题目传送门! luogu博客通道! 这题要用到错排,先理解一下什么是错排: 问题:有一个数集A,里面有n个元素 a[i].求,如果将其打乱,有多少种方法使得所有第原来的i个数a[i]不在原来 ...

  8. 洛谷P2480 [SDOI2010]古代猪文(费马小定理,卢卡斯定理,中国剩余定理,线性筛)

    洛谷题目传送门 蒟蒻惊叹于一道小小的数论题竟能涉及这么多知识点!不过,掌握了这些知识点,拿下这道题也并非难事. 题意一行就能写下来: 给定\(N,G\),求\(G^{\sum \limits _{d| ...

  9. [CodeVs1515]跳(lucas定理+费马小定理)

    嘿嘿嘿好久没写数学题了,偶尔看到一道写一写... 题目大意:一个(n+1)*(m+1)[0<=n, m<=10^12,n*m<=10^12]的矩阵,C(0,0)=1,C(x,y)=C ...

随机推荐

  1. spark和mapreduce的区别

    spark和mapreduced 的区别map的时候处理的时候要落地磁盘 每一步都会落地磁盘 reduced端去拉去的话 基于磁盘的迭代spark是直接再内存中进行处理 dag 执行引擎是一个job的 ...

  2. Redis常用命令详细介绍

    一.字符串 字符串键是Redis最基本的键值对类型,将一个单独的键和一个单独的值关联起来.通过字符串键,不仅可以存储和读取字符串,如果输入能被解释为整数和浮点数,还能执行自增或自减操作. 1.SET: ...

  3. double涉及大数据的时候会变成科学计数法

    double b=1.23456789128E8DecimalFormat df = new DecimalFormat("0.00");//精度自己控制保留几位小数点 Strin ...

  4. .net core webapi搭建(3)Code first+拆层三层+仓储

    将项目拆层 我们要 将项目拆分成 Infrastructure     基础层 Core                   核心层 Utility                  工具 我们想在就 ...

  5. [洛谷P2962] [USACO09NOV] 灯Lights

    Description Bessie and the cows were playing games in the barn, but the power was reset and the ligh ...

  6. C0nw4y's L!f3 G4me 代码实现

    这是我转载的博客,关于这个游戏的介绍.估计没人能get到这个游戏的blingbling的地方吧.还是蛮惊叹的. 因为这里网络实在惨淡,闲来无事实现了下这个游戏,UI尽量美化了,可惜python配置不知 ...

  7. Dart语言学习(六) Dart 列表List数组

    创建List : var list = [1,2,3,"Dart",true]; 创建不可变List : var list = const [1,2,3,"Dart&qu ...

  8. A complex 16-Level XSS Challenge

    A complex 16-Level XSS Challenge, held in summer 2014 (+1 Hidden Level) Index Level 0 Level 1 Level ...

  9. Excel.Application使用手册

    Excel.Application组件使用方法,适合应用于使用EXCEL组件做WEB应用开发. 转自http://bbs.xtjc.com/thread-376095-1-1.html 定制模块行为( ...

  10. Docker和Kubernetes

    Docker和Kubernetes Docker Docker是一个容器的开放平台,但它不是最早的.自20世纪70年代以来,容器平台一直存在.他们的开发可以追溯到Unix中的chroot系统调用.在2 ...