1067 - Combinations
Time Limit: 2 second(s) Memory Limit: 32 MB

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


Problem Setter: Jane Alam Jan
思路:费马小定理。
这个是组合数取模,有卢卡斯定理可以解决,但还没学,所以用费马小定理和快速幂水了一发。
当然先打表求阶乘取模,然后根据组合数公式Cnm=(m!)/((n!)*(m-n)!);
由于所给的数是1000003,素数,(n!)*(m-n)!,不能整除,根据(p/q)%N=k%N;其中k为所要求的数,
那么可以得到(p)%N=k*q%N;所以用费马小定理求下q的逆元就可以了,复杂度(N*log(N));
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<math.h>
5 #include<stdlib.h>
6 #include<string.h>
7 using namespace std;
8 typedef long long LL;
9 const long long N=1e6+3;
10 long long MM[1000005];
11 long long quick(long long n,long m);
12 int main(void)
13 {
14 long long p,q;MM[0]=1;
15 MM[1]=1;int i,j;
16 for(i=2;i<=1000000;i++)
17 {
18 MM[i]=(MM[i-1]%N*(i))%N;
19 }int v;
20 scanf("%d",&v);
21 for(j=1;j<=v;j++)
22 {scanf("%lld %lld",&p,&q);
23 long long x=MM[q]*MM[p-q]%N;
24 long long cc=quick(x,N-2);
25 long long ans=MM[p]*cc%N;
26 printf("Case %d: ",j);
27 printf("%lld\n",ans);
28 }
29 return 0;
30 }
31
32 long long quick(long long n,long m)
33 {
34 long long k=1;
35 while(m)
36 {
37 if(m&1)
38 {
39 k=(k%N*n%N)%N;
40 }
41 n=n*n%N;
42 m/=2;
43 }
44 return k;
45 }

1067 - Combinations的更多相关文章

  1. Light OJ 1067 Combinations (乘法逆元)

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

  2. LightOJ - 1067 - Combinations(组合数)

    链接: https://vjudge.net/problem/LightOJ-1067 题意: Given n different objects, you want to take k of the ...

  3. light oj 1067 费马小定理求逆元

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1067 1067 - Combinations Given n differen ...

  4. lightoj刷题日记

    提高自己的实力, 也为了证明, 开始板刷lightoj,每天题量>=1: 题目的类型会在这边说明,具体见分页博客: SUM=54; 1000 Greetings from LightOJ [简单 ...

  5. Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  6. [LeetCode] Factor Combinations 因子组合

    Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...

  7. [LeetCode] Combinations 组合项

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  8. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. 本地无法启动MySQL服务,报的错误:1067,进程意外终止

    在本地计算机无法启动MYSQL服务错误1067进程意外终止 这种情况一般是my.ini文件配置出错了 首先找到这个文件: 默认安装路径 C:/Program Files/MySQL/MySQL Ser ...

随机推荐

  1. 关于Stream的使用

    引言 Stream 是 Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复杂的查找.过滤和映射数据等操作.使用Stream API 对集合数据进行操作,就类似于使用 ...

  2. LeetCode二维数组中的查找

    LeetCode 二维数组中的查找 题目描述 在一个 n*m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增.请完成一个搞笑的函数,输入这样的一个二维数组和一个整数,判断数 ...

  3. C/C++ Qt 数据库与ComBox多级联动

    Qt中的SQL数据库组件可以与ComBox组件形成多级联动效果,在日常开发中多级联动效果应用非常广泛,例如当我们选择指定用户时,我们让其在另一个ComBox组件中列举出该用户所维护的主机列表,又或者当 ...

  4. 大数据学习day14-----第三阶段-----scala02------1. 元组 2.类、对象、继承、特质 3.函数(必须掌握)

    1. 元组 映射是K/V对偶的集合,对偶是元组的最简单的形式,元组可以装着多个不同类型的值 1.1 特点 元组相当于一个特殊的数组,其长度和内容都可变,并且数组中可以装任何类型的数据,其主要用处就是存 ...

  5. RB-Tree深度探索

    关联式容器就是通过key值来寻找value,这个和数据库很相像,为了提升查找效率,因此关联式容器底层大多数用红黑树或哈希表来实现. 红黑树是高度平衡的二叉树,它也被称为平衡二元搜索树. 如上所示,正常 ...

  6. C语言中的使用内存的三段

    1.正文段即代码和赋值数据段 一般存放二进制代码和常量 2.数据堆段 动态分配的存储区在数据堆段 3.数据栈段 临时使用的变量在数据栈段 典例 若一个进程实体由PCB.共享正文段.数据堆段和数据栈段组 ...

  7. IDEA2021.2安装与配置

    https://blog.csdn.net/qq_37242720/article/details/119349394

  8. Android Https相关完全解析

    转载: 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/48129405: 本文出自:[张鸿洋的博客] 一.概述 其实这篇文章理论 ...

  9. Output of C++ Program | Set 1

    Predict the output of below C++ programs. Question 1 1 // Assume that integers take 4 bytes. 2 #incl ...

  10. Playing with Destructors in C++

    Predict the output of the below code snippet. 1 #include <iostream> 2 using namespace std; 3 4 ...