CODEFORCES 272D Dima and Two Sequences

传送门:http://codeforces.com/problemset/problem/272/D


题意:给你两个串(a1,1),(a2,2),...,(an,n)和(b1,1),(b2,2),...,(bn,n)。把两个串放在一个集合里面,按照前一个参数排序,问有多少种排列方法?


分析:排列组合

对于(x,y),对于同一个x可能有许多对应的y,但y数一样中相同的最多两个。对于x分段求组合数,之后连乘,段中,有几对相同的就除以多少个2。(排列组合中去重而已)


code 1

#include
#include
#include 
#include 
#include 
#define ll long long

using namespace std;
vector > pq;
int main()
{
    int n,d;
    ll m;
    ll ans = 1;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&d);
        pq.push_back(make_pair(d,i));
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&d);
        pq.push_back(make_pair(d,i));
    }
    cin >> m;
    sort(pq.begin(),pq.end());
    ll dd = 0;
    for(int i=0;i< 2*n;i++)
    {
        int f = pq[i].first;
        int s = pq[i].second;
       i++;
        int f2 = pq[i].first;
        int s2 = pq[i].second;
        while(f == f2)
        {
           if(s == s2)
            {
               dd++;
            }

            f = f2;
            s = s2;
            i++;
            f2 = pq[i].first;
            s2 = pq[i].second;
        }
       if(i == 2*n-1) break;
        i--;
   }
   for(int i=0;i< 2*n;i++)
    {
       int f = pq[i].first;
      i++;
        int f2 = pq[i].first;
        ll a = 1;
        int x,tmp = 1;
        while(f == f2)
        {
            tmp++;
            x = tmp;
            while(dd&&x%2==0)
            {
                dd --;
                x/=2;

            }
            a = (a*x)%m;
           f = f2;
            i++;
            f2 = pq[i].first;
        }
        ans = ans*a%m;
        if(i == 2*n-1) break;
        i--;
   }
   printf("%I64d\n",ans);
   return 0;
}

code 2

#include
using namespace std;
typedef long long ll;
const int MAX=1e5+9;
ll a[MAX],n,m,b[MAX],cnt,ans=1;
map mp;
int main()
{
	cin>>n;
	for (int i=0;i>a[i],mp[a[i]]++;
	for (int i=0;i>b[i],cnt+=(b[i]==a[i]),mp[b[i]]++;
	cin>>m;
	for (auto i:mp)
		for (int j=1;j<=i.second;j++)
		{
			ll x=j;
			while (cnt>0 && x%2==0) x/=2,cnt--;
			ans=ans*x%m;
		}
	cout<



你可能感兴趣的:(数学:数论)