L. Lottery Tickets(结论+枚举)

传送门


1.题目大意:给出0~9每个数字的个数,问这些数排列起来能组成的最大的能整除4的整数是多少

2.首先必须知道一个重要的性质:

一个长度大于等于2的十进制整数 x x x,如果能整除某个非零个位数 y y y,那么只需满足 x x x的最后两位能整除 y y y

我太菜了,不会证明,实际上打个1000个数的表就能看出来了…

那么我们容易想到,枚举100以内的4的倍数,注意是作为二位数表示,即"00,04,08,12…",最后求出所有的数中最大的即可,但是有以下几个细节需要注意:

  • 当只有0时,直接输出0,这样就避免了前导0的出现
  • 当一共只有1个数字时,如果为4则输出4,为8则输出8
  • 最后无法构造时,还需特判一下是否能构造个位数的0,4,8,比如只有两个数分别为1和8,或者是1和0等等。这个时候无需判断是否同时存在0,4,8其中的多个,如果存在在上面就能成功构造出答案了
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <bitset>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define lowbit(x) (x&(-x))
#define mkp(x,y) make_pair(x,y)
#define mem(a,x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=2e5+10;

int a[20],b[20];

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n;
    cin>>n;
    while(n--){
        int sum=0;
        for(int i=0;i<10;i++){
            cin>>b[i];
            sum+=b[i];
        }
        if(sum==b[0]){
            cout<<"0\n";
            continue;
        }
        if(sum==1 && b[4]==1){
            cout<<"4\n";
            continue;
        }
        if(sum==1 && b[8]==1){
            cout<<"8\n";
            continue;
        }
        string ans="0";
        for(int i=0;i<25;i++){
            int m=i*4,x,y;
            memcpy(a,b,sizeof b);
            x=m/10,y=m%10;
            if(x==y && a[x]>=2){
                a[x]-=2;
                string s="";
                for(int j=9;j>=0;j--)
                    for(int k=0;k<a[j];k++)
                        s+=j+'0';
                s+=x+'0',s+=y+'0';
                ans=max(ans,s);
            }else if(x!=y && a[x] && a[y]){
                a[x]--,a[y]--;
                string s="";
                for(int j=9;j>=0;j--)
                    for(int k=0;k<a[j];k++)
                        s+=j+'0';
                s+=x+'0',s+=y+'0';
                ans=max(ans,s);
            }
        }
        if(ans[0]!='0'){
            cout<<ans<<"\n";
        }else{
            if(a[0]) cout<<"0\n";
            else if(a[4]) cout<<"4\n";
            else if(a[8]) cout<<"8\n";
            else cout<<"-1\n";
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值