CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » August 2010 Challenge » Swarm of Polygons

Swarm of Polygons

Problem code: SWARM

  • All Submissions

All submissions for this problem are available.

There is a regular n-gon. Some points are marked on each of its sides. There are x1 point marked on the first side, x2 on the second, , xn on the nth. The marked points do not coincide with the vertices of the n-gon. You can choose no more than one of the marked points from each side and form a convex non-degenerate polygon by connecting all those points with lines. Now your task is to find the number of different k-gons that can be formed that way.

Input

The first line of input file contains positive integer t the amount of test cases. Next t lines contain six integers each: n, k, a, b, c, m. Here n is the number of sides of the initial n-gon. The amount of marked points on the first side of this n-gon is x1 = a, the amount of the marked points on the following sides is xi = (b*xi-1 + c) mod m, for i > 1.

Constraints

1 <= t <= 30
3 <= n <= 109
3 <= k <= 20
1 <= b, c, m <= 106
0 <= a < m

Output

For each test case output the number of k-gons that can be formed modulo 1000000007.

Example

Input:
2
4 3 1 2 2 191
10 5 1 113 157 999991

Output:
1228
328836201



Author: spooky
Date Added: 13-04-2010
Time Limit: 10 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

what is the meaning of non

v_new.c @ 1 Aug 2010 04:41 PM

what is the meaning of non degenerate?

anyone please explain.

 

@atul the vertices do not

spooky @ 1 Aug 2010 04:55 PM

@atul

the vertices do not coincide, the adjacent sides do not form a 180 or 0 degree angle. generally it's just a normal polygon

is the reqd.. output (no. of

bbsgbbsg @ 1 Aug 2010 06:41 PM

is the reqd.. output (no. of k-gons)mod1000000007

 

@vijay yes

spooky @ 1 Aug 2010 07:11 PM

@vijay

yes

Can the official solution

triplem @ 4 Aug 2010 02:44 PM

Can the official solution handle 30 worst-case tests in under 10 seconds, or does it make some assumptions that the input will never be anything near that worst case? (And if so, what assumptions?)

@Stephen Yes, it can. No

spooky @ 4 Aug 2010 09:52 PM

@Stephen

Yes, it can. No assumptions. Everything in the statement.

Is O(m*k^2) enough to pass

pdwd @ 11 Aug 2010 03:57 PM

Is O(m*k^2) enough to pass the tests? I didn't manage to code this in time.

Mine was O(mk + k^2).@Ivan

triplem @ 11 Aug 2010 04:12 PM

Mine was O(mk + k^2).

@Ivan Mistreanu: your comment really confused me. Even the fastest submission for this problem doesn't come close to running 30 worst-case tests within the time limit (1000000000 20 1 5 1 999983). I asked because a simple loop like this:

for (int i=0; i<30; i++)
for (int j=0; j<20; j++)
for (int k=0; k<999983; k++) {
// any line like one multiplication mod m
}

doesn't run in 10 seconds on the judge, let alone an actual solution. I was 99% sure that there couldn't possibly be a faster solution than O(mk), but mine took about 2-3 seconds on the judge per test case in the worst case, so I was very surprised to see it passing.

Can't wait to see the official solution, I'm either going to be extremely impressed or extremely disappointed..

I am curious what the

subra @ 11 Aug 2010 04:26 PM

I am curious what the official solution is. Mine was O(m*k + k^3) and i had to get down and dirty with asm to get it correct :)

@Stephen, same case  here, I

subra @ 11 Aug 2010 04:33 PM

@Stephen, same case  here, I did similar timing tests and felt that it depended on the input distribution. Ivan's comment not withstanding, I figured my solution was just a little bit slower. So, asm it was for me.

This is what i figured out: long long modulus is VERY expensive compared to integer modulus. gcc generates a library function call __moddi3 apparently. But, in the restricted case that the modulus is a 32 bit integer (the MOD), you can actually do modulus of 64 bit dividend and 32 bit divisor in one instruction. That got me running in time.

i had O(m*k+k^2) and i was

imrankane2005 @ 11 Aug 2010 04:43 PM

i had O(m*k+k^2) and i was getting TLE in C++ but  when i changed it to C it got AC.

O(m*k + k*k*ln n) is enough

burdakovd @ 11 Aug 2010 08:58 PM

O(m*k + k*k*ln n) is enough to pass all tests, while O(m*k + k*k*1000) is not enough.

It is confusing that someone need optimize second part of solution (O(k*k*1000)), while in worst case (some example is "1000000000 20 1 1 1 1000000") slowest part will be m*k, which is not possible to run for 30 test cases within 10 seconds.

@Stephen Sorry, that this

spooky @ 11 Aug 2010 09:58 PM

@Stephen

Sorry, that this confused you. You probably will be dissapointed. I also doubt there is a solution better than O(mk). I can only say that there was a test case close to maximum test case and we sey the TL about 1.5 times of the official solution.

OK, so the official solution

triplem @ 12 Aug 2010 02:11 AM

OK, so the official solution can't handle the worst case like you said it did :( Can I request that in future the author's solution can solve the problem as stated; I spent many many hours trying to work out how to make my program faster than O(mk) since I knew that had no hope of passing based on the constraints + what you said.

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com