LOGIN
  • Register
  • Forgot Password?

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
  • COMPETE
    • March Algorithm Challenge
    • February Algorithm Challenge
    • January Algorithm Challenge
    • December Algorithm Challenge
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • About Directi
    • Careers
Home » Compete » Codechef Campus Snackdown » Alternating Permutations

Alternating Permutations

Problem code: SNCK03

  • All Submissions

All submissions for this problem are available.


[Problem Idea : Varun Jalan]

You are given K indices, A[1], A[2], ... , A[K].

A[1] < A[2] < ... < A[K].

A[1] = 1 and A[K] = N.

A permutation of the numbers between 1 and N is called valid if :

The numbers in the permutation between indices A[1] and A[2] (inclusive) form an increasing sequence, the numbers in the permutation between indices A[2] and A[3] (inclusive) form a decreasing sequence, those between A[3] and A[4] (inclusive) form an increasing sequence and so on.

Count the number of valid permutations.

Input:

There will be multiple test cases. The first line contains the number of test cases T. There follow 2*T lines, 2 lines for each test case. The first line for each test case contains the numbers N and K. The second line contains K space seperated numbers, ie. A[1] to A[K].

Output:

Output T lines, one for each test case. All answers should be output modulo 1000000007.

Constraints :

T <= 111

2 <= N <= 20000

2 <= K <= 22

K <= N

A[1] < A[2] < ... < A[K].

A[1] = 1 and A[K] = N.

Time Limit : 20 seconds.

Sample Input :

3
3 3
1 2 3
4 3
1 3 4
10 6
1 2 5 7 8 10

Sample Output :

2
3
6166

Note :

For the first test case,

(1 3 2) and (2 3 1) are the valid permutations.

For the second test case,

(1 2 4 3), (1 3 4 2) and (2 3 4 1) are the valid permutations.


Date: 2009-11-13
Time limit: 20

s
Source limit: 50000
Languages: C C99 strict C++ 4.0.0-8 C++ 4.3.2 PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp SCALA HASK ERL CAML CLPS PRLG WSPC BF ICK JS


  • Submit

Comments

  • Login or Register to post a comment.

I am not able to sign in from

Shitij Bhargava - 21st Nov,2009 19:09:57.
I am not able to sign in from my USITHawks account correctly. When I (or my team mates) try the site shows us to be signed in from some other account........please fix it asap, coz we'll not be able to make submissions till then......

in second test case 'k' no.

STL_Tweakers - 21st Nov,2009 19:38:48.
in second test case 'k' no. of elemnts in an array are 3 then how '1 2 3 4' came in it voilates the value of k as it should have 3 elemnts and permutation formed are of 4 elemnts

6166 permutations for 6

Bharat_Divye_Swagat - 21st Nov,2009 19:43:51.

6166 permutations for 6 numbers? only 720 permutations are possible...

@ Shitij You need to sign in

Aniruddha (Codechef) - 21st Nov,2009 20:06:07.

@ Shitij

You need to sign in from your team account.

The second test case is

Aniruddha (Codechef) - 21st Nov,2009 20:12:04.

The second test case is correct.

There are K=3 indices given and the line following it contains the indices values.

For k=2,shouldn't the 2nd

random - 21st Nov,2009 20:34:54.

For k=2,shouldn't the 2nd permutation be  (1 4 3 2) instead of (1 3 4 2) ??

behen ke lode english seekh

BUSTED - 21st Nov,2009 20:57:25.

behen ke lode english seekh phir humse question kar vaiyo madar chode bhosdi kay........!!!!!!!!!

behen ke lode english seekh

BUSTED - 21st Nov,2009 20:57:28.

behen ke lode english seekh phir humse question kar vaiyo madar chode bhosdi kay........!!!!!!!!!

the max indice A[K]=N

BBDNITM - 21st Nov,2009 21:22:29.

the max indice A[K]=N possible is 20,000...does that mean that the max length of the permutation is 20,000 !

@ BBDNITM You have to output

Aniruddha (Codechef) - 21st Nov,2009 21:29:39.

@ BBDNITM

You have to output your answer modulo 1000000007

For k=2,shouldn't the 2nd

random - 21st Nov,2009 21:34:33.

For k=2,shouldn't the 2nd permutation be  (1 4 3 2) instead of (1 3 4 2) ??

what is meant by modulo

arun garg - 21st Nov,2009 21:48:06.

what is meant by modulo 1000000007

@arun garg : divide the count

Varun Jalan - 21st Nov,2009 22:23:53.

@arun garg : divide the count by 1000000007 and take the remainder. thats the answer.

@ random : are you refering

Varun Jalan - 21st Nov,2009 22:38:20.

@ random : are you refering to the 2nd test case ?

yup....for 2nd case

random - 21st Nov,2009 22:40:29.

yup....for 2nd case 1(increase)->4 (decrease)-> 3

instead of what is given 1(increase)->3(increase)->4..

@ random I think the given

BigRed - 21st Nov,2009 22:57:31.

@ random

I think the given sample output is correct for the second case you mentioned.

(1 3 4 2) makes sense as 1, 3, 4 is an increasing subsequence in indices [1..3]

and 4, 2 is a decreasing subsequence in indices [3..4]

 

Can someone change our

fpmchu@gmail.com - 21st Nov,2009 23:06:49.

Can someone change our display name from my email address to our actual team name, "Audrey Horne"?

I can't do it with "Edit Profile".

Thanks,

Frank

Can someone change our

fpmchu@gmail.com - 21st Nov,2009 23:26:17.

Can someone change our display name from my email address to our actual team name, "Audrey Horne"?

I can't do it with "Edit Profile".

Thanks,

Frank

cc

Rohit Gupta - 23rd Feb,2010 07:04:13.

cc

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Fetching successful submissions
  • About CodeChef
  • About Directi
  • CEO's Corner
  • Careers
  • feedback@codechef.com
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
Sponsors
The time now is: