HDU5225 Tom and permutation (permutation combination)

Source: Internet
Author: User

Test instructions

Tom learned to write a program to find out the inverse logarithm of a 1-n arrangement, but his teacher gave him a problem: give a 1-n arrangement, and ask for the sum of the inverse logarithm of the order of all the 1-n that the dictionary order is smaller. Tom didn't know what to do at the moment, so he came to you to help him solve the problem. Because the number may be large, the answer is
   
    
          9+7  
 

    
   Take the mold.
Before pushing backwards, calculate all permutations of 1-k to produce the total number of reverse order,

First assume that db[2] is the result of 1-2, then we look at the arrangement of 3, he is composed of 1[2,3],2[1,3],3[1,2] three items, "" for all permutations meaning, the first item in the "2,3" combination to the result of the contribution of the DB "2", the front 1 is less than the back two number, no contribution, a total of db "2" The second "1,3" combination contributes db "2" to the result, and the front 2 to the back of 1 always produces 1 reverse order, a total of db "2" +2! The second "Up" combination contributes db "2" to the result, and the preceding 3 pairs always produce 2 reverse order, a total of db "2" +2*2! The continuation of the push will be able to roll out all permutations of 1-k to produce reverse totals.

Then look at a sample 3 3 2 1, which has a sequence of

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

QQ (I,J) calculates the number of digits that are smaller than J after the I bit,

So the first two is a group that produces 2! *QQ (+DB) "2" reverse order, third to four to produce 2! *QQ (+DB) "2" in reverse order, the last group to use a similar number of methods to calculate the number and then statistics, the code will be clearer than I said.

Code:

#pragma COMMENT (linker, "/stack:102400000,102400000") #include <iostream> #include <cstdio> #include < string> #include <cmath> #include <queue> #include <stack> #include <map> #include < cstring> #include <fstream> #include <algorithm> #define REP (i,a,b) for (Long Long i= (a);i< (b); i++) # Define REV (I,a,b) for (int i= (a); i>= (b); i--) #define CLR (a,x) memset (a,x,sizeof a) #define INF 0x3f3f3f3ftypedef long Long ll;using namespace Std;const double eps=10e-10;const LL mod=1000000007; const int Maxn=105;const int maxm=600005;    ll C (ll x) {if (x==0) return 0;    LL ret=1;        while (x>1) {ret=ret*x%mod;    x--; } return ret;}    LL da[105],db[105],sum[105],done[105];void Inn () {db[2]=1;        Rep (i,3,105) {db[i]=db[i-1];    Rep (j,1,i) db[i]= (Db[i]+j*c (i-1) +db[i-1])%mod; }}ll N;    LL QQ (ll A,ll b) {ll ans=0;    Rep (i,a+1,n) if (da[i]<b) ans++; return ans;}    int main () {Inn (); while (~SCANF ("%i64d", &n)) {Rep (i,0,n) scanf ("%d", &da[i]);        LL ans=0;clr (sum,0); CLR (done,0);            Rep (i,0,n) {done[da[i]]=1;            Rep (J,1,da[i]) if (!done[j]) sum[i]+=c (n-1-i);        Rep (J,1,da[i]) if (!done[j]) ans= (Ans+db[n-1-i]+c (n-1-i) *qq (i,j))%mod;            } for (int i=n-2;i>=0;i--) {sum[i]+=sum[i+1];        Ans= (ANS+SUM[I+1]*QQ (i,da[i))%mod;    } printf ("%i64d\n", ans); } return 0;}




HDU5225 Tom and permutation (permutation combination)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.