Codeforces 584D. Dima and Lisa Solution

Solve in  C++;
  1. ///**********ALLAH IS ALMIGHTY************///
  2. ///AH Tonmoy
  3. ///Department of CSE,23rd Batch
  4. ///Islamic University,Bangladesh
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7. bool isprime(int no)
  8. {
  9. int sq=sqrt(no);
  10. for(int i=2; i<=sq; i++)
  11. {
  12. if(no%i==0)
  13. {
  14. return false;
  15. }
  16. }
  17. return true;
  18. }
  19. main()
  20. {
  21. int r,f,i,j,a;
  22. int n;
  23. while(scanf("%d",&n)!=EOF)
  24. {
  25. f=0;
  26. if(isprime(n))
  27. {
  28. cout<<"1"<<endl;
  29. cout<<n<<endl;
  30. }
  31.  
  32. else
  33. {
  34. for(i=n; i>1; i--)
  35. {
  36. if(isprime(i))
  37. {
  38. a=i;
  39. break;
  40. }
  41.  
  42. }
  43. r=n-a;
  44. if(r%2==1||r==2)
  45. {
  46.  
  47. cout<<"2"<<endl;
  48. cout<<r<<" "<<a<<endl;
  49. }
  50. else
  51. {
  52. for(i=2; i<r-1; i++)
  53. {
  54. for(j=r-1; j>1; j--)
  55. {
  56. if(isprime(i))
  57. {
  58. if(isprime(j))
  59. {
  60. if(i+j==r)
  61. {
  62. cout<<"3"<<endl;
  63. cout<<i<<" "<<j<<" "<<a<<endl;
  64. f=1;
  65. break;
  66. }
  67. if(f==1)
  68. break;
  69. }
  70. if(f==1)
  71. break;
  72. }
  73. if(f==1)
  74. break;
  75. }
  76. if(f==1)
  77. break;
  78.  
  79. }
  80.  
  81. }
  82.  
  83. }
  84. }
  85. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.