Advertisement
mickypinata

USACO-T012: Prime Cryptarithm

Sep 21st, 2021
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. /*
  2. ID: mickyta1
  3. TASK: crypt1
  4. LANG: C++
  5. */
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. const int N = 10;
  11.  
  12. int arr[N + 1];
  13. bool isInSet[10];
  14.  
  15. bool checkSet(int x){
  16.     while(x > 0){
  17.         int lastDigit = x % 10;
  18.         if(!isInSet[lastDigit]){
  19.             return false;
  20.         }
  21.         x /= 10;
  22.     }
  23.     return true;
  24. }
  25.  
  26. bool has3Digit(int x){
  27.     return 100 <= x && x < 1000;
  28. }
  29.  
  30. bool has4Digit(int x){
  31.     return 1000 <= x && x < 10000;
  32. }
  33.  
  34. int main(){
  35.  
  36.     freopen("crypt1.in", "r", stdin);
  37.     freopen("crypt1.out", "w", stdout);
  38.  
  39.     int n;
  40.     scanf("%d", &n);
  41.     for(int i = 1; i <= n; ++i){
  42.         scanf("%d", &arr[i]);
  43.         isInSet[arr[i]] = true;
  44.     }
  45.     int cnt = 0;
  46.     for(int a = 1; a <= n; ++a){
  47.         if(arr[a] == 0){
  48.             continue;
  49.         }
  50.         for(int b = 1; b <= n; ++b){
  51.             for(int c = 1; c <= n; ++c){
  52.                 int fst = 100 * arr[a] + 10 * arr[b] + arr[c];
  53.                 for(int d = 1; d <= n; ++d){
  54.                     if(arr[d] == 0 || !has3Digit(fst * arr[d])){
  55.                         continue;
  56.                     }
  57.                     for(int e = 1; e <= n; ++e){
  58.                         int snd = 10 * arr[d] + arr[e];
  59.                         if(arr[e] == 0 || !has3Digit(fst * arr[e]) || !has4Digit(fst * snd)){
  60.                             continue;
  61.                         }
  62.                         if(checkSet(fst * arr[d]) && checkSet(fst * arr[e]) && checkSet(fst * snd)){
  63.                             ++cnt;
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     cout << cnt << '\n';
  71.  
  72.     fclose(stdin);
  73.     fclose(stdout);
  74.  
  75.     return 0;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement