CodeChef: Codechef
Site under maintenance
  • Register
  • Forgot Password?
  • PRACTICE
    • Easy
    • Medium
    • Hard
  • COMPETE
    • August Mini Challenge
    • August Algorithm Challenge
    • July Algorithm Challenge
    • June Algorithm Challenge
    • May Gamers Challenge
  • DISCUSS
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef TechTalks
    • CodeChef Meetups
    • Campus Chapters
    • Host Your Contest
    • User Groups
  • HELP
    • Frequently Asked Questions
    • FAQ for Problem Setters
    • Tutorial: Paying Up
    • Tutorial: Small Factorials
    • Tutorial: Input and Output(I/O)
    • Tutorial: Your First Non-Trivial Problem
  • ABOUT
    • About CodeChef
    • About Directi
    • CEO's Corner
    • Press Room
    • Careers

Home » Problems (hard) » Algebra Express

Algebra Express

Problem code: ACMKANPE

  • Submit
  • All submissions

All submissions for this problem are available.

E
Algebra Express
Input file: stdin
Output file: stdout

Mim is a high school algebra teacher. Just last week, she taught her students how to evaluate an expression. For example, if

r = a × (b + c) − d × e

Given the values of a, b, c, d and e, they can evaluate r.

Now, to test how well her students are able to do these arithmetic, she has to prepare some test. But, she do this every year, and each time she has to make same type of questions. That made her a little bored. She thinks that evaluating an expression only depends on the position of operators and variables, not the variables itself. For example in the expression above, for all possible values of a, b, c, d and e, they will all be equally hard to evaluate.

She has made some generic expressions, and a set of values, which she may use in any order (e.g. one time she may use a = 2, b = 5, another time she may use a = 5, b = 2). Now, she wants to know, the number of possible ways to assign these values, so that, the value of the expression lies within a specified limit.

Input

First line contains T, the number of test cases.

Each test case starts with three integers N(2 ≤ N ≤ 12), l, h (−1012 ≤ l ≤ h ≤ 1012), the number of variables, lower limit and the upper limit of the acceptable expression values.

Next line contains N integers ai(0 ≤ ai ≤ 9), the value of the variables she will be using in the exam.

Next line contains the expression, where each variable is represented as ‘x’. The expression will be well formed, and will contain only the symbols ‘+’, ‘-’, ‘*’,‘(’,‘)’ and ‘x’.

Output

For each test case, output the number of different assignments, that produce results within the limit [l, h].

Note that, you will be replacing each ‘x’ with a value aj(1 ≤ j ≤ N). Each aj have to be used exactly once. Two assignments are considered the same, if, after replacing all ‘x’, the expressions look the same.

Sample input and output



stdin
stdout


3

3 1 10

1 2 5

x+x*x

4 1 10

1 2 2 5

(x+x)*(x-x)

4 1 100

1 1 1 1

x*(x-(x*x))

4

4

0




Date:2010-10-27
Time limit:20s
Source limit:50000B
Languages:PERL6 CLOJ GO PYTH 3.1.2 F# C C99 strict C++ 4.3.2 JAVA TEXT


  • Submit

Comments

Loading Comments...

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Loading Submissions...

RECENT ACTIVITY FOR THIS PROBLEM:

Loading Recent Activity...

HELP

Program should read from standard input and write to standard output.

After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.

If you are still having problems, see a sample solution here.

  • 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: