CodeForces - 375A(同余)

题目链接:https://vjudge.net/problem/CodeForces-375A

题目大意:给你一串数,必定包含1,6,8,9,让你输出一个能被7整除的数

  因为必定包含1689四个数,我们可以发现1689组成的全排列刚好对7取模得到0~6,所以我们就根据前面(瞎搞)出来的数的余数选择出合适的1689的排列再在末尾补0就是了,比如说给你111689000,我们对11取模,根据同于定理得:余数=((1%7)*10+1%7)=4, 那么如果再接上4位0的话,余数就是4000%7 = 3, 这时候我们只要找一个由1689组成的数%7=3即可,比如说1986,那么结果就是111986了,最后我们在末尾补上0,得到111986,就是我们最后的结果了

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 1e6+10;
char str[maxn];
char str2[][10] = {"1869", "6198", "1896", "1689", "1986", "1968", "1698"};
int cnt[10];
int main(void) {
    while(~scanf("%s", str)) {
        int len = strlen(str);
        for (int i = 0; i<len; ++i)
            ++cnt[str[i]-'0'];
        cnt[1]-=1, cnt[6]-=1, cnt[8]-=1, cnt[9]-=1;
        ll sum = 0;
        for (int i = 1; i<10; ++i)
            while(cnt[i]) {
                putchar(i+'0');
                --cnt[i];
                sum = (sum*10+i)%7;
            }
        printf("%s", str2[sum]);
        while(cnt[0]) {
            --cnt[0];
            putchar('0');
        }
        putchar(endl);
    }
    return 0;
}

 

posted @ 2020-03-03 11:23  shuitiangong  阅读(198)  评论(0编辑  收藏  举报