EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. Meta Coding Competitions
  2. 2011
  3. Round 2

Scott's New Trick

https://www.facebook.com/codingcompetitions/hacker-cup/2011/round-2/problems/B

PreviousBonus AssignmentsNextStudious Student II

Last updated 2 months ago

Question

Little Scott recently learned how to perform arithmetic operations modulo some prime number P. As a training set, he picked two sequences a of length N and b of length M, generated in the following way:

  1. a1=A1a_1 = A1a1​=A1

  2. a2=A2a_2 = A2a2​=A2

  3. ai=(ai+2∗A3+ai−1∗A4+A5)a_i = (a_{i+2}*A3+a_{i-1}*A4+A5)ai​=(ai+2​∗A3+ai−1​∗A4+A5) modmodmod PPP, for i=3..Ni=3..Ni=3..N

  4. b1=B1b_1 = B1b1​=B1

  5. b2=B2b_2 = B2b2​=B2

  6. bj=(bj+2∗B3+bj−1∗B4+B5)b_j = (b_{j+2}*B3+b_{j-1}*B4+B5)bj​=(bj+2​∗B3+bj−1​∗B4+B5) modmodmod PPP, for j=3..Mj=3..Mj=3..M

Now he wants to find the number of pairs (i,j)(i, j)(i,j), were 1≤i≤N1 ≤ i ≤ N1≤i≤N and 1≤j≤M1 ≤ j ≤ M1≤j≤M, such that (ai∗bj)(a_i * b_j)(ai​∗bj​) modmodmod P<LP < LP<L, for given number L. He asked you to do the same to help him check his answers.

Input Format

The first line of input file consists of a single number T, the number of test cases. Each test consists of three lines. The first line of a test case contains two integers: prime number P and positive integer L. The second line consists of six non-negative integers N, A1, A2, A3, A4, A5. Likewise, the third line contains six non-negative integers M, B1, B2, B3, B4, B5.

Constraints

T=20T = 20T=20
  • P is guaranteed prime

Output Format

Output T lines, with the answer to each test case on a single line.

Sample Inputs:

Input

5
3 1
4 0 2 2 2 2
2 1 2 1 0 0
3 1
5 2 0 0 1 1
5 1 1 2 0 0
3 3
5 0 0 1 2 2
3 2 1 1 1 1
5 1
5 2 0 4 0 4
3 2 1 2 4 4
5 4
2 2 1 3 1 4
5 1 0 2 3 3

Output

6
10
15
3
9

Solution

2≤P<250,0002 ≤ P < 250,0002≤P<250,000
1≤L≤P1 ≤ L ≤ P1≤L≤P
2≤N,M≤10,000,0002 ≤ N, M ≤ 10,000,0002≤N,M≤10,000,000
0≤A1,A2,A3,A4,A5,B1,B2,B3,B4,B5<P0 ≤ A1, A2, A3, A4, A5, B1, B2, B3, B4, B5 < P0≤A1,A2,A3,A4,A5,B1,B2,B3,B4,B5<P