c ++ sequence

Title Description

Life, most things are in order, because the order of the United States is the most intoxicating. So now RCDH saw things ring true headache. So he wants the world to become orderly, but he was just an unknown junior, so I had to start with a sequence of numbers.

His knowledge clutter sequence is "reverse order" number of decision equation is Q = 2 ^ n, where Q refers to the degree of confusion, n being a sequence refers to the "reverse order" number. Reverse order is defined as: assuming the i-th sequence is ai, if present, i <j, such that ai> aj, then <ai, aj> on a reverse pair.

Your task is given a sequence, calculate the degree of confusion Q. This number may be relatively large, you only need to output Q% 1991's.

Entry

The first row, the integer n, the sequence has a number n.

Second row, an n

Export

Only one row, the value of Q mod 1991's.

Sample input

4
1 3 4 2

Sample Output

4

prompt

Note: There are two samples in reverse order, so Q = 2 ^ 2 = 4. Therefore, Q mod 1991 = 4.
For 30% of the data 2 = <n <= 1000
to 100% of the data 2 = <n <= 50000
each count the column does not exceed 10 million of positive integers.

Source Code

#include<iostream>
using namespace std;
long long tot,ans,temp[50005],a[50005];
void qsort(int l,int r)//基数排序
{
    if(l==r) return;
    int mid=(l+r)/2;
    qsort(l,mid);
    qsort(mid+1,r);
    int p=l,i=l,j=mid+1;
    while(i<=mid&&j<=r)
    {
        if(a[i]>a[j])
        {
            tot+=mid-i+1;//逆序对的公式 
            temp[p++]=a[j++];
        }
        else
            temp[p++]=a[i++];
    }
    while(i<=mid) 
        temp[p++]=a[i++];
    while(j<=r) 
        temp[p++]=a[j++];
    for(i=l;i<=r;i++) 
        a[i]=temp[i];
}
int pow(int n)//求幂次方
{
    int ans1=1,a=2;
    while(n>0)
    {
        if(n%2==1)
            ans1=(ans1*a)%1991;
        n/=2; 
        a=(a*a)%1991;
    }
    return ans1;
}
int main()
{
    int n = 0;
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }
    qsort(1,n);
    ans=pow(tot);
    cout<<ans<<endl;//输出
    return 0;
}

Guess you like

Origin www.cnblogs.com/LJA001162/p/11334891.html