CF 337A (Puzzles-Brute Force enumeration)

Source: Internet
Author: User

A. Puzzles
Time limit per test1 second
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
The end of the school year is near and Ms. manana, the teacher, will soon have to say goodbye to a yet another class. she decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces ).

The shop assistant told the teacher that there are m puzzles in the shop, but they might differ in difficulty and size. specifically, the first jigsaw puzzle consists of f1 pieces, the second one consists of f2 pieces and so on.

Ms. manana doesn' t want to upset the children, so she decided that the difference between the numbers of pieces in her presents must be as small as possible. let A be the number of pieces in the largest puzzle that the teacher buys and B be the number of pieces in the smallest such puzzle. she wants to choose such n puzzles that A random-duplicate B is minimum possible. help the teacher and find the least possible value of A random-Parallel B.

Input
The first line contains space-separated integers n and m (2 ≤ limit n ≤ limit m ≤ limit 50 ). the second line contains m space-separated integersf1, limit f2, limit ,..., when fm (4 Gbit/S ≤ memory fi ≤ memory 1000)-the quantities of pieces in the puzzles sold in the shop.

Output
Print a single integer-the least possible difference the teacher can obtain.

Sample test (s)
Input
4 6
10 12 10 7 5 22
Output
5
Note
Sample 1. the class has 4 students. the shop sells 6 puzzles. if Ms. manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. it is impossible to obtain a smaller difference. note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5.

 

 

 

Sort and find the difference between the first and last differences in the brute force mode.

 

#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (1000000009)long long mul(long long a,long long b){return (a*b)%F;}long long add(long long a,long long b){return (a+b)%F;}long long sub(long long a,long long b){return (a-b+abs(a-b)/F*F+F)%F;}typedef long long ll;ll n,m,k;ll pow2(ll a,int b){if (b==0) return 1;if (b==1) return a%F;ll p=pow2(a,b/2);p=p*p%F;if (b%2) p=p*a%F;return p;}int main(){//freopen("Quiz.in","r",stdin);//freopen(".out","w",stdout);//For(i,100) cout<<pow2(2,i)<<' ';cin>>n>>m>>k;ll m1=m;m=n-m;int maxk=n/k;if (m>=maxk) {cout<<m1%F<<endl;return 0;}else m=maxk-m;ll am=sub((4*pow2(2,m-1))%F,2);ll ans=add(am*k%F,m1-k*m);cout<<ans%F<<endl;return 0;}


 

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.