B - Makes And The Product CodeForces - 817B(组合数)

2024-04-16 03:18

本文主要是介绍B - Makes And The Product CodeForces - 817B(组合数),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn’t been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i,  j,  k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!

Input
The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.

Output
Print one number — the quantity of triples (i,  j,  k) such that i,  j and k are pairwise distinct and ai·aj·ak is minimum possible.

Examples
Input
4
1 1 1 1
Output
4
Input
5
1 3 2 3 4
Output
2
Input
6
1 3 3 1 3 2
Output
1
Note
In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4.

In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2.

In the third example a triple of numbers (1, 1, 2) is chosen, and there’s only one way to choose indices.

题意: 不同坐标三个数相乘得到的最小值,有多少种选法
思路: 组合数?不过本题只需要排序以后选前3个数,那么选法变多只有可能第三个数多了很多相同的情况。不过怕漏选,直接全部考虑了一下,然后套组合数公式即可

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>using namespace std;
typedef long long ll;const int maxn = 2e5 + 7;
ll a[maxn];
map<ll,int>mp;ll C[maxn][10];
const int N = 100000 + 5;
const ll MOD = (int)1e18 + 7;
void init()
{C[1][0] = C[1][1] = 1;for (int i = 2; i < N; i++){C[i][0] = 1;for (int j = 1; j < 6; j++)C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]);}
}int main()
{init();int n;scanf("%d",&n);for(int i = 1;i <= n;i++){scanf("%lld",&a[i]);mp[a[i]]++;}sort(a + 1,a + 1 + n);ll ans = 0;if(a[1] != a[2] && a[1] != a[3] && a[2] != a[3]){ans = mp[a[1]] * mp[a[2]] * mp[a[3]];printf("%lld\n",ans);}else if(a[1] == a[2] && a[2] == a[3]){int num = mp[a[1]];printf("%lld\n",C[num][3]);}else if(a[1] == a[2]){int num = mp[a[1]];ll ans = C[num][2];ans *= 1ll * mp[a[3]];printf("%lld\n",ans);}else if(a[2] == a[3]){int num = mp[a[2]];ll ans = C[num][2];ans *= 1ll * mp[a[1]];printf("%lld\n",ans);}else if(a[1] == a[3]){int num = mp[a[1]];ll ans = C[num][2];ans *= 1ll * mp[a[2]];printf("%lld\n",ans);}return 0;
}

这篇关于B - Makes And The Product CodeForces - 817B(组合数)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907667

相关文章

hdu4869(逆元+求组合数)

//输入n,m,n表示翻牌的次数,m表示牌的数目,求经过n次操作后共有几种状态#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#includ

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

Go组合

摘要 golang并非完全面向对象的程序语言,为了实现面向对象的继承这一神奇的功能,golang允许struct间使用匿名引入的方式实现对象属性方法的组合 组合使用注意项 使用匿名引入的方式来组合其他struct 默认优先调用外层方法 可以指定匿名struct以调用内层方法 代码 package mainimport ("fmt")type People struct{}type Pe

组合c(m,n)的计算方法

问题:求解组合数C(n,m),即从n个相同物品中取出m个的方案数,由于结果可能非常大,对结果模10007即可。       共四种方案。ps:注意使用限制。 方案1: 暴力求解,C(n,m)=n*(n-1)*...*(n-m+1)/m!,n<=15 ; int Combination(int n, int m) { const int M = 10007; int

代码随想录训练营day37|52. 携带研究材料,518.零钱兑换II,377. 组合总和 Ⅳ,70. 爬楼梯

52. 携带研究材料 这是一个完全背包问题,就是每个物品可以无限放。 在一维滚动数组的时候规定了遍历顺序是要从后往前的,就是因为不能多次放物体。 所以这里能多次放物体只需要把遍历顺序改改就好了 # include<iostream># include<vector>using namespace std;int main(){int n,m;cin>>n>>m;std::vector<i