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. 使用SpringBoot实现文件的上传

    使用SpringBoot实现文件的上传 springboot可以直接使用 org.springframework.web.multipart.MultipartFile 所以非常容易实现 一.首先是简 ...

  2. 使用Postman轻松实现接口数据关联

    Postman Postman是一款非常流行的HTTP(s)接口测试工具,入门简单,界面美观,功能强大.作为一个测试/开发工程师,这是一款必须要会用的工具.今天以一个实际的案例,来介绍下Postman ...

  3. YARP+AgileConfig 5分钟实现一个支持配置热更新的代理网关

    YARP 是微软开源的一个反向代理项目,英文名叫 Yet Another Reverse Proxy .所谓反向代理最有名的那就是 nginx 了,没错 YARP 也可以用来完成 nginx 的大部分 ...

  4. Elaticsearch(一)--基础原理及用法

    一.基础概念 1.Elasticsearch简介 Lucene是Java语言编写的全文(全部的文本内容进行分析,建立索引,使之可以被搜索)检索引擎工具包(全文检索引擎的架构),用于处理纯文本的数据,提 ...

  5. C++ 害死人不偿命的(3n+1)猜想

    第一次刷PAT ,注意事项:就像普通编译器一样要导入头文件 还有 using namespace std:要不然会报错(鬼知道我经历了什么 微笑.jpg) 1 #include <iostrea ...

  6. 100个Shell脚本——【脚本3】tomcat启动脚本

    [脚本3]tomcat启动脚本 一.脚本tomcatd.sh #!/bin/bash # chkconfig:2345 64 36 # description: Tomcat start/stop/r ...

  7. 【编程思想】【设计模式】【其他模式】hsm

    Python版 https://github.com/faif/python-patterns/blob/master/other/hsm/hsm.py """ Impl ...

  8. springboot+vue集成mavon-editor,开发在线文档知识库

    先睹为快,来看下效果: 技术选型 SpringBoot.Spring Security.Oauth2.Vue-element-admin 集成mavon-editor编辑器 安装 mavon-edit ...

  9. JConsole可视化工具

    JConsole基本介绍 Jconsole (Java Monitoring and Management Console),一种基于JMX的可视化监视.管理工具.JConsole 基本包括以下基本功 ...

  10. 【Spring Framework】Spring入门教程(四)注册Bean到IOC容器

    注册Bean到IOC容器大致分为4种: ①.包扫描+组件注解(@Controller.@Service.@Repository.@Component) 针对类是我们自己编写的情况 ②.@Bean注解 ...