Codeforce Round #221 Div2 C
You have number a, whose decimal representation quite luckily contains digits 1, 6, 8, 9. Rearrange the digits in its decimal representation so that the resulting number will be divisible by 7.
Number a doesn't contain any leading zeroes and contains digits 1, 6, 8, 9 (it also can contain another digits). The resulting number also mustn't contain any leading zeroes.
The first line contains positive integer a in the decimal record. It is guaranteed that the record of number a contains digits: 1, 6, 8, 9. Number a doesn't contain any leading zeroes. The decimal representation of number a contains at least 4 and at most 106 characters.
Print a number in the decimal notation without leading zeroes — the result of the permutation.
If it is impossible to rearrange the digits of the number a in the required manner, print 0.
1689
1869
18906
18690
1 #pragma comment(linker,"/STACK:102400000,102400000") 2 #include <cstdio> 3 #include <vector> 4 #include <cmath> 5 #include <queue> 6 #include <cstring> 7 #include <iostream> 8 #include <algorithm> 9 using namespace std; 10 #define INF 0x7fffffff 11 #define mod 1000000007 12 #define ll long long 13 #define maxn 1000005 14 #define pi acos(-1.0) 15 int n, m; 16 char s[maxn]; 17 int a[maxn]; 18 bool cmp(int a, int b){ return a > b; } 19 int main(){ 20 scanf("%s", s); 21 n = strlen(s); 22 memset(a, -1, sizeof a); 23 sort(s, s + n,cmp); 24 if (s[4] == '0'||n==4){ 25 printf("1869"); 26 for (int i = 0; i < n - 4; i++)printf("0"); 27 return 0; 28 } 29 for (int i = 0; i < n; i++){ 30 if (s[i] == '1'&&a[0] == -1)a[0] = i; 31 if (s[i] == '6'&&a[1] == -1)a[1] = i; 32 if (s[i] == '8'&&a[2] == -1)a[2] = i; 33 if (s[i] == '9'&&a[3] == -1)a[3] = i; 34 } 35 int x = 0; 36 for (int i = 0; i < n; i++){ 37 if (i == a[0] || i == a[1] || i == a[2] || i == a[3])continue; 38 printf("%c", s[i]); 39 x = x * 10 + (s[i] - '0'); 40 x = x % 7; 41 } 42 int t = x; 43 for (int i = 0; i < 4; i++) 44 for (int j = 0; j < 4; j++) 45 for (int x = 0; x < 4; x++) 46 for (int y = 0; y < 4; y++) 47 if (i != j&&i != x&&i != y&&j != x&&j != y&&x != y){ 48 if ((t * 10000 + (s[a[i]] - '0') * 1000 + (s[a[j]] - '0') * 100 + (s[a[x]] - '0') * 10 + (s[a[y]] - '0')) % 7 == 0){ 49 printf("%c%c%c%c", s[a[i]], s[a[j]], s[a[x]], s[a[y]]); 50 return 0; 51 } 52 } 53 }
浙公网安备 33010602011771号