-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaparitii.cpp
47 lines (39 loc) · 1015 Bytes
/
aparitii.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("aparitii.in");
ofstream out("aparitii.out");
// digitFrec[digitPos][digit] show the number of times digit
// appears in position digitPos (counted form right to left)
unsigned digitFrec[10][10];
void ProcessNumber(unsigned number) {
size_t digitPos = 0;
while(number) {
digitFrec[digitPos++][number % 10]++;
number/=10;
}
}
// This works because if we have a digit appear in a position
// a number of times which is not divisible by k, then that digit
// has that position in the missing number we are trying to find.
unsigned MissingNumber(unsigned k) {
unsigned number = 0;
for(int digitPos = 9; digitPos >= 0; --digitPos) {
number *= 10;
for(unsigned digit = 0; digit < 10; ++digit) {
if(digitFrec[digitPos][digit] % k != 0)
number += c;
}
}
return number;
}
int main() {
unsigned n,k;
in >> n >> k;
for(size_t i = 0; i < n; ++i) {
unsigned a;
in >> a;
ProcessNr(a);
}
out << MissingNumber(k);
}