fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5.  
  6. bool check_length (int K, int N) {
  7. int length = 0;
  8. while (K) {
  9. K /= 10;
  10. length++;
  11. }
  12. return length==N;
  13. }
  14.  
  15. bool isPrime (int K) {
  16. if (K==2 || K==3) return true;
  17. if (K<2 || K%2==0) return false;
  18. for (int i=2; i<=sqrt(K); i++) {
  19. if (K%i==0) return false;
  20. }
  21. return true;
  22. }
  23.  
  24. bool check_sum (int K) {
  25. int sum = 0;
  26. while (K>0) {
  27. sum += K%10;
  28. K /= 10;
  29. }
  30. return isPrime(sum);
  31. }
  32.  
  33. bool check_digit (int K) {
  34. while (K>0) {
  35. if (!isPrime(K%10)) return false;
  36. K /= 10;
  37. }
  38. return true;
  39. }
  40.  
  41. int main () {
  42. cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
  43.  
  44. int K, N;
  45. cin >> K >> N;
  46.  
  47. if (check_length(K, N) && isPrime(K) && check_sum(K) && check_digit(K)) {
  48. cout << "YES";
  49. }
  50. else {
  51. cout << "NO";
  52. }
  53. return 0;
  54. }
Success #stdin #stdout 0s 5280KB
stdin
23 2
stdout
YES