Choose My own algorithm 2012 CCC Canadian High School student Information Science Orsay

Source: Internet
Author: User

In Watertown, you may see some geese. How did you see the goose on the calculator? by typing 35336 and then turning the calculator upside down, you can see the geese--Goose.

Now the minus sign of your calculator is no force, only the + and x numbers can be used, and only some of the numbers can work. The task now is to get a given target value for this crappy calculator with a simple number and a fixed number of operations.

Note that the operation of this calculator is executed immediately when it is pressed, without the need to follow the rules of operator precedence. (see Example 2)

Enter a description

The first line enters an integer w, indicating the number of operations you can and must use. W is an integer between 0 and 6. The line that is being closed is an integer D (1≤d≤10), the number of number keys that can work. The next D-Line gives the D-working numeric key, which is a non-repeating integer between 0-9. Then give an integer v (1≤v≤5), that is, the number of target values, followed by a line of V in each row from 0 to 5,000,000 (inclusive) of an integer, representing the value you need to calculate the calculator.

Output description

The output contains a V line, which corresponds to the V target value. Each row is Y or n, which means that the corresponding target value can be computed or not computed in the case of using the D-number and just as the W-time operation. To be precise, a target value T can be calculated by starting with a number from the D-digit clock, by adding or multiplying its numbers just w times, and then finally getting the value T. The numbers can be reused. Numbers do not need to be exhausted. Note, however, that you cannot enter a multi-digit number.

Sample input Sample Input

Sample Input 1

6

3

6

7

8

1

35336

Sample Input 2

3

2

4

9

2

97

88

Sample output Sample Output

Sample Output 1:

Y

Sample Output 2:

N

Y

Data range and Tips Data Size & Hint

For the second data, although 4x4+9x9 = 97, the order of the calculations is done sequentially from left to right, so output n. On the second line, 9+9+4*4 got 88.

The first impression of this topic can be directly searched
Up to 6 operators 7 digits with a total state of 2 of 6 squares multiplied by 10 square
This number is much larger than the computer can accept a total of 100 million ...

We can notice any one answer, it must be from the previous number Multiply or addand come
Then we can search backwards, if the current count can be removed or added, then we will proceed in addition to and minusRecursion until the W time (or W plus minus 1) has been recursive? times) to see if the resulting number is feasible. This way our state number is greatly reduced, the saturation number is probably about 50000000*6, but we cannot run so many times because of the existence of pruning, the estimation algorithm can guarantee that the state number is significantly below 1000000. )

#include <iostream>

using namespace Std;

int ans,w;

int N;

int V;

BOOL q[100000000];

int temp;

BOOL Can (int ans,int k)

{

if (k==0)

{

if (ans<=9)

{

if (Q[ans])

return true;

Else

return false;

}

Else

return false;

}

for (int i=0;i<=9;i++)

if (Q[i])

if (Can (ANS-I,K-1))

return true;

for (int i=1;i<=9;i++)

if (ans%i==0)

if (Q[i])

if (Can (ANS/I,K-1))

return true;

if (ans==0)

if (Can (0,K-1))

return true;

return false;

}

int main ()

{

cin>>w;

cin>>n;

for (int i=1;i<=n;i++)

{

cin>>temp;

Q[temp] = true;

}

cin>>v;

for (int i=1;i<=v;i++)

{

cin>>ans;

if (Can (ANS,W))

cout<< "Y" <<endl;

Else

cout<< "N" <<endl;

}

return 0;

}

Choose My own algorithm 2012 CCC Canadian High School student Information Science Orsay

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.