题目链接: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. 点分治 (等级排) codeforces 321C

    Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected ...

  2. Linux初始化Git环境

    第一步:设置Git全局用户名和邮箱 git config --global user.name "你的用户名" git config --global user.email &qu ...

  3. electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint

    我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...

  4. 基于selenium爬取京东

    爬取iphone 注意:browser对象会发生变化,当对当前网页做任意操作时 import time from selenium import webdriver from selenium.web ...

  5. ajxa的TypeError: $.ajax is not a function的冲突问题

    在加载onclick的方法异步过程中,浏览器报错 首先自我检查 原因一:没有加载Jquery库,原因二:$.ajax没有在$(function(){$.ajax();})中. 发现都不是 原因三:有没 ...

  6. 微服务的多数据源配置: step 1

    spring boot + mybatis: 实现的功能点: 多数据源 jdbc: spring.datasource.test1.url = jdbc:mysql://localhost:3306/ ...

  7. 微软的github 上面 有 Docker.DotNet 嗯 作为 菜 只有欣赏的额

    .NET Client for Docker Remote API step one 需要下载的 猛戳 Docker.DotNet

  8. JS-04-流程控制和循环

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. LeetCode-指针法

    LeetCode刷题总结-指针法 方法介绍:指针法主要使用在一组按从小到大排好序的数组中,当按照条件查找对应元素时,在数组的前后定义两个指针,当两个指针代表的元素进行运算时:若结果大于目标值,则左移右 ...

  10. mysql--->权限管理原理和设置

    mysql 权限管理 mysql权限检查原理 权限检查两个阶段 你有没有权限链接上来 你有没有权限执行此操作 服务器如何判断用户用户有没有权限连接上来? 通过mysql库下的user表 查看:sele ...