Codevs 1229 Number Games

Source: Internet
Author: User

Transmission Door

Description

Lele was bored in class recently, so he invented a number game to pass the time. The game is like this, first of all, he took out a few pieces of paper, respectively, 0 to 9 of any number (can be repeated to write a number), then he asked the classmate casually write two numbers x and K. The thing Lele to do is to re-spell the cards, make the number T, and T + X is the positive integer multiple of K. Sometimes, when a lot of paper, Lele often can't spell in a class, but he wants to know the answer, so, he would like to ask you to help write a program to calculate the answer.

Input

    • The first line contains two integers N and M (0<n<9,0<m<2000), each representing the number of pieces of paper and the number of queries.
    • The second line contains n integers representing the numbers written on the paper, each of which may take 0~9.
    • Next there are M-line queries, each asking for two integers x and K (0<=x<10^9,0<k<100).

Output

    • For each query, if you can use these pieces of paper to spell out the answer to the T, the output of the result T. If there are multiple results, the minimum t that meets the requirements is output.
    • If it cannot be spelled out, the output is "None".

Sample Input

4 3

1 2 3 4

5 7

33 6

12 8

Sample Output

1234

None

1324

Ideas

enumerates the full permutations of the n number of inputs. But this ran more than 300ms, Dfs seems as long as 25ms, will not write deep search orzzz

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int n,m,x,k;bool flag;int pow (int x,int N) {int res = 1;while (n) {if (n & 1) {res *= X;} X *= x;n >>= 1;} return res;}        void permutation (int n,int *p,int *a,int cur)//input array P, and output in dictionary order all the full permutations of the elements of array A {if (cur = = n) {int T = 0;        for (int i = 0;i < n;i++) T + = A[i]*pow (10,n-i-1);        printf ("%d\n", T);        if ((t+x)% K = = 0) {flag = true;for (int i = 0;i < n;i++) i?printf ("%d", A[i]):p rintf ("%d", a[i]);        printf ("\ n"); }} else for (int i = 0; i < n; i++) {if (!i | |            P[i]! = P[i-1]) {int C1 = 0,C2 = 0;            for (int j = 0; j < cur; j + +) if (a[j] = = P[i]) c1++;            for (int j = 0; J < N; j + +) if (p[i] = = P[j]) c2++;                if (C1 < C2) {A[cur] = P[i];                Permutation (n,p,a,cur+1);   if (flag) return;         }}}}int Main () {int p[15],a[15];scanf ("%d%d", &n,&m), for (int i = 0;i < n;i++) scanf ("%d", &am P;p[i]); while (m--) {scanf ("%d%d", &x,&k), flag = False;permutation (n,p,a,0), if (!flag) printf ("none\n");}}

  

Codevs 1229 Number Games

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.