0
点赞
收藏
分享

微信扫一扫

CF 337A(Puzzles-暴力枚举)



A. Puzzles



time limit per test



memory limit per test



input



output


n

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

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 - B is minimum possible. Help the teacher and find the least possible value of A - B.


Input



n and m (2 ≤ n ≤ m ≤ 50). The second line contains m space-separated integersf1, f2, ..., fm (4 ≤ fi ≤ 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.




排序,暴力找前后差m-1个的差即可。



#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;
}







举报

相关推荐

0 条评论