1229 digital games

Source: Internet
Author: User

Description

Lele was bored recently in class, so he invented a digital game to pass the time. This game is like this. First, he took out a few pieces of paper and wrote any number between 0 and 9 (a number can be re-rewritten). Then, he asked his classmates to write two numbers, X and K. What Lele wants to do is to fight these cards again to form a digital T, and t + X is a positive integer multiple of K. Sometimes, when there are a lot of pieces of paper, Lele often cannot be spelled out in a lesson, but he wants to know the answer, so he wants to ask you to write a program to calculate the answer.

Input description input description

The first line of 1 contains two integers, N and M (0 <n <9, 0 <m <2000), representing the number of pieces of paper and the number of inquiries respectively.

2 The second row contains N integers representing the numbers written on the paper. Each number may be 0 ~ 9.

3. Then there are m rows for each query. Each query is given two integers x and K (0 <= x <10 ^ 9,0 <k <100 ).

Output description

4. For each query, if the T matching the answer can be spelled out using the paper, The result t will be output. If multiple results exist, the minimum t meeting the requirements is output.

5. If it cannot be spelled out, "NONE" is output ".

Sample Input

4 3

1 2 3 4

5 7

33 6

12 8

Sample output sample output

1234

None

1324

 

// Train of thought: the list of N numbers.

#include<iostream>#include<vector>#include<algorithm>using namespace std;bool test(const vector<int> &v,int x,int k){int result=0;for(int i=0;i<v.size();i++){result=10*result+v[i];}if((result+x)%k==0) return true;else return false;}int main(){int n,m;cin>>n>>m;vector<int> v(n);for(int i=0;i<n;i++) cin>>v[i];for(int i=0;i<m;i++){int x,k;cin>>x>>k;sort(v.begin(),v.end());bool flag=false;do{if(test(v,x,k)){flag=true;break;}}while(next_permutation(v.begin(),v.end()));if(flag==false) {cout<<"None"<<endl;continue;}for(int i=0;i<v.size();i++){cout<<v[i];}cout<<endl;}}


 

1229 digital 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.