【牛客 - 181F】子序列(容斥,组合数,费马小定理)

 

题干:

题目描述

给出一个长度为n的序列,你需要计算出所有长度为k的子序列中,除最大最小数之外所有数的乘积相乘的结果

 

输入描述:

第一行一个整数T,表示数据组数。
对于每组数据,第一行两个整数N,k,含义如题所示接下来一行N个整数,表示给出的序列保证序列内的数互不相同

输出描述:

对于每组数据,输出一个整数表示答案,对取模
每组数据之间以换行分割

 

示例1

输入

复制

3
4 3 
5 3 1 4
5 4
3 7 5 2 1
10 3 
100 1020 2050 102 12 235 4 57 32135 54354 

输出

复制

144
81000
521918013

说明

 

第一组数据解释

所有长度为3的子序列为

最终答案为

备注:

对于的数据:
对于的数据:
对于的数据:
保证序列中的元素互不相同且,

解题报告:

首先暴力可以n^2。思路就是要算每一个数的贡献,也就是每一个数的出现次数。

总次数是C(n-1,k-1)。我们可以枚举   在前面选了多少个,在后面选了多少个。但是这样复杂度就成了O(nk)了,,也高不了哪去。所以想个办法优化,不难想到,可以求问题的反面,容斥一下,用总次数减去作为最大最小值次数。可以排个序,和前面搭配是作为最大值,和后面搭配作为最小值。

注意,以前组合数是乘数,现在组合数是指数,不要习惯性对p=1e9+7取模。

指数可以对p-1取模。因为这里p是质数,a^(p-1)=1 mod p(费马小定理)

所以,指数减掉若干个p-1,并不影响。

当一般地,(a,p)=1而p不是质数,就欧拉定理,a^(phi(p))=1 mod p所以对phi(p)取模

AC代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2005;
const int mod=1e9+7;
typedef long long ll;
int n,q,k;
ll a[N];
ll c[N][N];
ll qm(ll x,ll y) {ll ret=1;while(y) {if(y%2==1) ret=(ret*x)%mod;x=(x*x)%mod;y/=2;}if(ret<0) ret=(ret+mod)%mod;return ret%mod;
}
signed main() {for(int i=0; i<=2001; i++) {c[i][0]=1;for(int j=1; j<=i; j++) {c[i][j]=(c[i-1][j-1]+c[i-1][j])%(mod-1);}}scanf("%lld",&q);while(q--) {scanf("%lld%lld",&n,&k);for(int i=1; i<=n; i++)scanf("%lld",&a[i]);sort(a+1,a+n+1);ll ans=1;for(int i=1; i<=n; i++) {ll ci=(c[n-1][k-1]-c[i-1][k-1]-c[n-i][k-1]+5*(mod-1))%(mod-1);ans=(ans*qm(a[i],ci))%mod;}printf("%lld\n",ans);}return 0;
}

参考博客:链接​​​​​​​

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

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

相关文章

【CodeForces - 485C】Bits (二进制相关,数学,贪心)

题干&#xff1a; Lets denote as the number of bits set (1 bits) in the binary representation of the non-negative integer x. You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, an…

php工程导致系统蓝屏,经常蓝屏是什么原因

经常蓝屏的原因&#xff1a;1、错误安装或更新显卡驱动后导致电脑蓝屏&#xff1b;2、超频过度是导致蓝屏&#xff1b;3、安装的软件存在不兼容&#xff1b;4、电脑内部硬件温度过高&#xff1b;5、内存条接触不良或内存损坏。错误更新显卡驱动错误安装或更新显卡驱动后导致电脑…

【CodeForces - 485A】Factory (水题,抽屉原理,tricks)

题干&#xff1a; One industrial factory is reforming working plan. The director suggested to set a mythical detail production norm. If at the beginning of the day there were x details in the factory storage, then by the end of the day the factory has to p…

matlab调用哈希表,ros与matlab联动使用

ros与matlab联动使用联动使用教程1 ubuntu18.04或16.04下安装matlab R2017b参考链接2 ubuntu下基于matlab启动rosmatlab中常用命令如下&#xff1a;查看robotics system toolbox工具箱是否安装成功help robotics.rosrosinit ——表示启动ROS&#xff0c;相当于roscorerosshutdo…

【CodeForces - 1020A】New Building for SIS(模拟)

题干&#xff1a; You are looking at the floor plan of the Summer Informatics Schools new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get …

【CodeForces - 1020B】Badge(模拟,图,环)

题干&#xff1a; In Summer Informatics School, if a student doesnt behave well, teachers make a hole in his badge. And today one of the teachers caught a group of nn students doing yet another trick. Lets assume that all these students are numbered from …

【Hihocoder - 1723】子树统计(线性基合并)

题干&#xff1a; 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵N个节点的有根树&#xff0c;树上每个节点有一个非负整数权重Wi。定义节点集合S{i1, i2, ……, ir}的总权重为&#xff1a;(⊕是异或运算) 每次询问一棵子树内所有可能出现的总权重数量&a…

【CodeForces - 485D】Maximum Value (枚举,用数组离散化,数学,取模运算,因子,筛法)

题干&#xff1a; You are given a sequence a consisting of n integers. Find the maximum possible value of (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj. Input The first line contains integer n — the length of the seq…

【牛客 - 371牛客OI周赛7-提高组A】小睿睿的等式(dp,暴力 )

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/371/A 来源&#xff1a;牛客网 小睿睿在游戏开始时有n根火柴棒&#xff0c;他想知道能摆成形如“ABn”的等式且使用的火柴棒数也恰好等于n/k的等式有多少种(BAn与ABn看作一种) 注&#xff1a; “”与…

oracle用户新增数据文件,[数据库]20200722_Oracle添加表空间、用户,用户授权

[数据库]20200722_Oracle添加表空间、用户&#xff0c;用户授权0 2020-07-25 17:00:30--创建表空间CREATE TABLESPACE aifu --表空间名 aifu LOGGING DATAFILE D:\dev_config\OracleTableSpaces\aifu.DBF SIZE 5M --数据文件路径 D:\dev_config\OracleTableSpaces AUTOEXTEND O…

【牛客 - 327牛客寒假算法基础集训营2 I】处女座的测验(二)(积性函数性质,数论,素数唯一性分解,STL)

题干&#xff1a; 链接&#xff1a;https://ac.nowcoder.com/acm/contest/327/I 来源&#xff1a;牛客网 现在处女座顺利的完成了测验&#xff0c;处女座想要知道知道自己输出的结果是否正确。他希望知道自己有自己输出的数中有多少对是不满足要求的。 更具体的&#xff0c…

linux监控命令执行,你可能不知道的 即时监控 Linux 使用者执行指令的三种方法...

原标题&#xff1a;你可能不知道的 即时监控 Linux 使用者执行指令的三种方法这里介绍如何在 Linux 系统上以管理者权限即时监控一般使用者所执行的任何指令。Linux 的 root 管理者可对系统进行任何的管理与操作&#xff0c;如果想要即时监控特定使用者在主机上所执行的指令&am…

【POJ - 2728】Desert King (最有比率生成树,分数规划)

题干&#xff1a; David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be…

linux远程打开windows程序,为新手讲解Linux和Windows系统的远程桌面访问知识

很多新手都是使用Linux和Windows双系统的&#xff0c;它们之间的远程桌面访问是如何连接的&#xff0c;我们就为新手讲解Linux和Windows系统的远程桌面访问知识&#xff0c;包括所使用的软件及方法。本文所使用的Linux版本是深度操作系统&#xff0c;如果要安装该版本请参考U盘…

【蓝桥杯官网试题 - 算法训练 】K好数(线性dp与优化)

题干&#xff1a; 问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字&#xff0c;那么我们就说这个数是K好数。求L位K进制数中K好数的数目。例如K 4&#xff0c;L 2的时候&#xff0c;所有K好数为11、13、20、22、30、31、33 共7个。由于这个数目很大…

【CodeForces - 897D】Ithea Plays With Chtholly (交互题型,贪心,思维构造,题目信息)

题目大意&#xff1a; This is an interactive problem. Refer to the Interaction section below for better understanding. Ithea and Chtholly want to play a game in order to determine who can use the kitchen tonight. Initially, Ithea puts n clear sheets of pa…

红帽子linux生效环境变量,RedHat Linux 5无法使用ifconfig的解决方法

ifconfig是Linux显示网络设备的命令&#xff0c;能够设置网络设备的状态&#xff0c;但在RedHat Linux 5系统中&#xff0c;有时会遇到ifconfig命令无法使用的情况&#xff0c;下面小编就给大家介绍下RedHat Linux 5无法使用ifconfig命令的解决方法。一起去看看吧&#xff01;在…

关于交互题---Idleness limit exceeded(codeforces)

Idleness limit exceeded的中文&#xff1a;懒惰超过限制。----来自有道翻译 大概意思是在该输出的情况下没有输出数据 比如这个&#xff1a; 而且一般都会提醒你&#xff1a;&#xff08;在每条输出之后加&#xff09; After outputting each line, dont forget to flush t…

Linux如何搭建服务器eb,Linux下CRMEB环境搭建

环境准备:PHP7.0.33MySQL5.7Apache2.4PHP环境安装:sudo apt-get install php-pear php7.0-cli php7.0-common php7.0-curl \php7.0-dev php7.0-fpm php7.0-json php7.0-mbstring php7.0-mcrypt \php7.0-mysql php7.0-opcache php7.0-zip php7.0-intl php7.0-gd php7.0-xmlMySQL…

linux socket ip层配置,Linux下Socket通信(TCP实现)

近期在做的项目中&#xff0c;涉及到了进程间数据传输&#xff0c;系统的原本实现是通过管道&#xff0c;但是原有的实现中两个进程是在同一台机器&#xff0c;而且两个进程的关系为父子关系&#xff0c;而我们要做的是将其中一个进程移植到服务器上&#xff0c;因此两个进程要…