排列组合或容斥原理

题目链接:https://vjudge.net/contest/237052#problem/H

Here we go!

Let‘s define the diversity of a list of numbers to be the difference between the largest and smallest number in the list.

For example, the diversity of the list (1, -1, 2, 7) = 7 - (-1) = 8.

A substring of a list is considered a non-empty sequence of contiguous numbers from the list. For example, for the list (1,3,7), the substrings are (1), (3), (7), (1,3), (3,7), (1,3,7). A subsequence of a list is defined to be a non-empty sequence of numbers obtained by deleting some elements from the list. For example, for the list (1,3,7), the subsequences are (1), (3), (7), (1,3), (3,7), (1,7), (1,3,7).

Given a list of length N find the number of substrings and subsequences in this list with the maximum diversity. If a substring/subsequence having maximum diversity occurs multiple times in the list, each of its occurances adds towards the answer.   And tell Harry Potter your answer.

Input (STDIN):

The first line contains T, the number of test cases. Then follow T test case blocks.

Each blocks starts with the first line containing the number N.

The second line contains a list of numbers in this list.

Output (STDOUT):

For each test case, output the number of substrings and the number of subsequences in this list with the maximum diversity.

Since the answers maybe very large, output them modulo 1000000007.

Constraints:

T <= 10

N <= 100,000

Each number in the list is between 1 and 100,000 inclusive.

Time Limit: 2 s

Memory Limit: 32 MB

Sample Input:

3

3

1 2 3

4

1 4 3 4

3

3 2 1

Sample Output:

1 2

3 6

12

Enough with this Harry Potter, please! What are we, twelve-year olds?  Let‘s get our teeth into some real pumpkin pasties -- oops, programming problems!

Here we go!

Let‘s define the diversity of a list of numbers to be the difference between the largest and smallest number in the list.

For example, the diversity of the list (1, -1, 2, 7) = 7 - (-1) = 8.

A substring of a list is considered a non-empty sequence of contiguous numbers from the list. For example, for the list (1,3,7), the substrings are (1), (3), (7), (1,3), (3,7), (1,3,7). A subsequence of a list is defined to be a non-empty sequence of numbers obtained by deleting some elements from the list. For example, for the list (1,3,7), the subsequences are (1), (3), (7), (1,3), (3,7), (1,7), (1,3,7).

Given a list of length N find the number of substrings and subsequences in this list with the maximum diversity. If a substring/subsequence having maximum diversity occurs multiple times in the list, each of its occurences adds towards the answer.   And tell Harry Potter your answer

Input (STDIN):

The first line contains T, the number of test cases. Then follow T test case blocks.

Each blocks starts with the first line containing the number N.

The second line contains a list of numbers in this list.

Output (STDOUT):

For each test case, output the number of substrings and the number of subsequences in this list with the maximum diversity.

Since the answers maybe very large, output them modulo 1000000007.

Constraints:

T <= 10

N <= 100,000

Each number in the list is between 1 and 100,000 inclusive.

Sample Input:

3

3

1 2 3

4

1 4 3 4

3

3 2 1

Sample Output:

1 2

3 6

1 2

题目大意:输入t,t组测试数据,输入n,有n个数,判断两个不同的字符串和序列串的标准是最大值减最小值不同,字符串必须是连续的,序列串可以不连续

问你有多少个最大的字符串和序列串

个人思路:因为所求一定是最大值减最小值,所以先把最大值和最小值记录下来。先看字符串怎么求,看下面例子

这是一个公式:sum=sum+min(t1,t2),t1,t2是最小最大值的下标,会有更新,刚开始都赋值为0

2 1 4 3 2 1   min=1,max=4,t1=0,t2=0;

i=1, t1=0,t2=0,sum=min(0,0)+sum=0;

i=2, t1=2,t2=0,sum=min(2,0,)+sum=0;

i=3, t1=2,t2=3,sum=min(2,3)+sum=2,有2 1 4和1 4

i=4, t1=2,t2=3,sum=min(2,3)+sum=4, 有2 1 4 3和1 4 3

i=5, t1=2,t2=3,sum=min(2,3)+sum=6,有2 1 4 3 2和1 4 3 2

i=6, t1=6,t2=3,sum=min(6,3)+sum=9,有2 1 4 3 2 1和1 4 3 2 1 和4 3 2 1

那么我们来看为什么会有这个公式呢,加上本身就不用多说了,那么为什么是加上min(t1,t2)呢,因为从后面每增加一个数,都可以在第一个到min(t1,t2)个数开始到末尾

都是一个字符串,所以增加的就是min(t1,t2)

然后要求序列串,有两种求法,一种是排列组合,还要一种是容斥原理。需要算出有多少个最大值和最小值

假设最大值max_num个,最小值min_num个,总共有n个

排列组合的公式是:(2^min_num-1)*(2^max_num-1)*(2^(n-min_num-max_num)),为什么是这样,自己想一下,就能清楚

容斥原理的公式是:2^n-2^(n-min_num)-2^(n-max_num)+2^(n-min_num-max_num)

还有这题很坑,不能用I64d,原因我也不清楚,检查了很久,把输入输出改一下就过了,气气气·······

看代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
#define INF 0x3f3f3f
ll quickpow(ll a,ll b)
{
    //a的b次方
    ll res=a,ans=1;
    while(b)
    {
        if(b&1)
        {
            ans=ans*res%mod;
            res=res*res%mod;
        }
        else
            res=res*res%mod;
        b=b>>1;
    }
    return ans;
}
int main()
{
    ll t;
    ll a[100050];
    scanf("%lld",&t);
    //cin>>t;
    while(t--)
    {
        ll n,mi=INF,ma=-1,l=0,r=0,ans=0,ans1=0,min_num=0,max_num=0;//l代表最小值的下标,r代表最大值的下标
        scanf("%lld",&n);
        //cin>>n;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
           // cin>>a[i];
            if(a[i]>ma)
                ma=a[i];
            if(a[i]<mi)
                mi=a[i];
        }
        if(mi==ma)
        {
            ans=n*(n+1)/2%mod;
            ans1=quickpow(2,n)-1;
            printf("%lld %lld\n",ans,ans1);
           //cout<<ans<<‘ ‘<<ans1<<endl;
            continue;
        }
        for(int i=1;i<=n;i++)
        {
            if(a[i]==mi)
            {
                l=i;
                min_num++;//最小值个数
            }
            if(a[i]==ma)
            {
                r=i;
                max_num++;//最大值个数
            }
            ans=(ans+min(l,r))%mod;//子串个数
        }//注意这里要时刻%mod,不这样就wa,可能会爆
        ans1=(quickpow(2,min_num)-1)%mod*(quickpow(2,max_num)-1)%mod*(quickpow(2,n-min_num-max_num))%mod;
       // ans1=(quickpow(2,n)-quickpow(2,n-min_num)-quickpow(2,n-max_num)+quickpow(2,n-min_num-max_num))%mod;
        if(ans1<0)
            ans1=ans1+mod;
        printf("%lld %lld\n",ans,ans1);
        //cout<<ans<<‘ ‘<<ans1<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/caijiaming/p/9309349.html

时间: 2024-10-06 21:56:54

排列组合或容斥原理的相关文章

hdu 4497 GCD and LCM 质因素分解+排列组合or容斥原理

//昨天把一个i写成1了 然后挂了一下午 首先进行质因数分解g=a1^b1+a2^b2...... l=a1^b1'+a2^b2'.......,然后判断两种不可行情况:1,g的分解式中有l的分解式中没有的质因子 2,存在bi>bi',然后剩下的都是可行解,对于每一个质因子三个数中有两个分别bi,bi',第三个的取值可为[bi,bi'],所以对于每一个质因子共有6(bi-bi')种取法(A(2,3)*(b-a+1)+C(2,3)*2分别为取得值在和不在边界上的情况,特殊:如果bi=bi'就只有一

HDU 4497 GCD and LCM(分解质因子+排列组合)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满足要求的(x, y, z)有多少组,并且要考虑顺序. 思路:如果L%G != 0显然不存在这样的(x, y, z),相反肯定存在.具体做法就是将L/G分解质因子,得到:L/G = P1^t1 * P2^t2 * ... * Pk^tk,我们来考虑任意一个因子Pi^ti,此时(x/G, y/G, z/

[Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)

[Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数.要保证每行每列的格子上的数最小值为1,有多少种方案 \(n \leq 250,k \leq 10^9\) 分析 这题有\(O(n^3)\)的dp做法,但个人感觉不如\(O(n^2 \log n)\)直接用数学方法求更好理解. 考虑容斥原理,枚举有\(i\)行最小值>1,有\(j\)行最小值>1,那

HDU--5396(区间dp+排列组合)

做这道题的时候,想到会不会是dp,然后发现dp可做,但是一直被自己坑到死. 枚举最后合并的那个位置,然后对于加减号的,分成的前后两个部分都有不同的组合方法, (a1+a2........) +  (b1,b2.............)         对于每个a,被加b的个数的阶乘次 ,对于每个b,被加a的个数的阶乘次 减法同理 乘法特殊一点 (a1+a2........) *  (b1,b2.............)  乘法分配率,直接将两部分的总和相乘即可 想到这些还远远没有结束,因为最

排列组合

(常考)错位排列 有N封信和N个信封,每封信都不装在自己信封里的排列种数记作Dn,则 D1=0,D2=1,D3=2,D4=9,D5=44,D6=265 一.相邻问题---捆绑法 不邻问题---插空法 对于某几个元素不相邻的排列问题,可先将其他元素排好,再将不相邻元素在已排好的元素之间及两端空隙中插入即可. [例题1]一张节目表上原有3个节目,如果保持这3个节目的相对顺序不变,再添进去2个新节目,有多少种安排方法? A.20 B.12 C.6 D.4 [答案]A. [解析] 以下内容需要回复才能看

hdu 1799 (循环多少次?)(排列组合公式)

循环多少次? Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3051    Accepted Submission(s): 1117 Problem Description 我们知道,在编程中,我们时常需要考虑到时间复杂度,特别是对于循环的部分.例如, 如果代码中出现 for(i=1;i<=n;i++) OP ; 那么做了n次OP运算

排列组合问题

一.不同元素子集问题 78. Subsets Given a set of distinct integers, nums, return all possible subsets. 给定一组非重复数字,求出所有可能的子集 解析: 例如 [1,2,3],解法: 首先放[],然后往已有的[]中放1 1. 首先放1 此时已有[ [], 1 ] 2. 然后对[ [], 1 ] 放2 于是此时有 [ [], [1], [2], [1,2] ] 3. 然后对[ [], [1], [2], [1,2] ]

排列组合问题之圆形分布

1.问题1.1 团团坐有一张圆桌,坐了A,B,C,D四个人,已知,D在A的右边,C在D的对面,请问A,B,C,D,的坐次? 解答:这个问题相对简单,我们纸上画一画,就能画出他们的可能的位置了 但是,可能还有一种解,比如我们把A,B,C,D依次右转一个位,也是满足条件的,而且只要保持他们的相对位置不变,依次右转n个位都是问题的解,而且还有个有趣的事情,当他们转了一圈(即右转4个位)后,他们右回到原位了 2.圆形分布上面这个问题就是一种圆形分布,那么他和直线分布的区别在哪里呢?又有什么联系呢?上面文

【noi 2.6_9288】&amp;【hdu 1133】Buy the Ticket(DP / 排列组合 Catalan+高精度)

题意:有m个人有一张50元的纸币,n个人有一张100元的纸币.他们要在一个原始存金为0元的售票处买一张50元的票,问一共有几种方案数. 解法:(学习了他人的推导后~) 1.Catalan数的应用7的变形.(推荐阅读:http://www.cnblogs.com/chenhuan001/p/5157133.html).P.S.不知我之前自己推出的公式“C(n,m)*C(2*m,m)/(m+1)*P(n,n)*P(m,m)”是否是正确的. (1)在不考虑m人和n人本身组内的排列时,总方案数为C(m+