1.3.3Prime Cryptarithm

  
    
  1. /*
  2. ID: awsd1231
  3. PROG: crypt1
  4. LANG: C++
  5. */
  6. #include<iostream>
  7. #include<cstdio>
  8. using namespace std;
  9. int n, num[10] = {0}, equa[60000][5];
  10. int main() {
  11. freopen("crypt1.in", "r", stdin);
  12. freopen("crypt1.out", "w", stdout);
  13. cin >> n;
  14. for(int i = 0; i != n; ++i) {
  15. cin >> num[i];
  16. }
  17. int idx = 0;
  18. for(int a = 0; a != n; ++a) {
  19. for(int b = 0; b != n; ++b) {
  20. for(int c = 0; c != n; ++c) {
  21. for(int d = 0; d != n; ++d) {
  22. for(int f = 0; f != n; ++f) {
  23. equa[idx][0] = num[a];
  24. equa[idx][1] = num[b];
  25. equa[idx][2] = num[c];
  26. equa[idx][3] = num[d];
  27. equa[idx++][4] = num[f];
  28. } } } } }
  29. int count = 0;
  30. for(int i = 0; i != idx; ++i) {
  31. int a = equa[i][0]*100 + equa[i][1]*10 + equa[i][2];
  32. int b = equa[i][3] * 10 + equa[i][4];
  33. int w1 = a*b%10, w2 = a*b/10%10, w3 = a*b/100%10, w4 = a*b/1000%10;
  34. int l1 = a*equa[i][4]%10, l2 = a*equa[i][4]/10%10, l3 = a*equa[i][4]/100%10, l4 = a*equa[i][4]/1000;
  35. int t1 = a*equa[i][3]%10, t2 = a*equa[i][3]/10%10, t3 = a*equa[i][3]/100%10, t4 = a*equa[i][3]/1000;
  36. bool can[14] = {0};
  37. if(a*b > 9999 || a*equa[i][4] > 999) continue;
  38. for(int j = 0; j != n; j++) {
  39. if(!can[0] && (w1 == num[j])) can[0] = true;
  40. if(!can[1] && (w2 == num[j])) can[1] = true;
  41. if(!can[2] && (w3 == num[j])) can[2] = true;
  42. if(!can[3] && (w4 == num[j])) can[3] = true;
  43. can[4] = true;
  44. if(!can[5] && (l1 == num[j])) can[5] = true;
  45. if(!can[6] && (l2 == num[j])) can[6] = true;
  46. if(!can[7] && (l3 == num[j])) can[7] = true;
  47. if(!can[8] && (l4 == num[j] || l4 == 0)) can[8] = true;
  48. if(!can[9] && (t1 == num[j])) can[9] = true;
  49. if(!can[10] && (t2 == num[j])) can[10] = true;
  50. if(!can[11] && (t3 == num[j])) can[11] = true;
  51. if(!can[12] && (t4 == num[j] || t4 == 0)) can[12] = true;
  52. }
  53. can[13] = true;
  54. for(int i = 0; i != 13; ++i)
  55. if(!can[i]) can[13] = false;
  56. if(can[13]) count++;
  57. }
  58. cout << count << endl;
  59. return 0;
  60. }





你可能感兴趣的:(Prim)