Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. int n, m, a[42];
  6. int vis[42] , ok;
  7.  
  8. void fun( int cur, int step )
  9. {
  10. if( cur == n && step == m ){
  11. ok = 1;
  12. return;
  13. }
  14. if( vis[cur] != -1 ) return;
  15.  
  16. int x = a[cur];
  17. set<int>st;
  18.  
  19. for(int i=2; i*i <= x; i++){
  20. if( n%i == 0 ){
  21. st.insert(i);
  22. while( x%i == 0 ) x /= i;
  23. }
  24. }
  25. if( x > 1 ) st.insert(x);
  26.  
  27. for(auto i:st ){
  28. if( cur-i >= 1 ){
  29. vis[cur-i] = 1;
  30. fun( cur-i , step+1 );
  31. }
  32.  
  33. if( cur+i <= n ){
  34. vis[cur+i] = 1 ;
  35. fun( cur+i , step+1 );
  36. }
  37. }
  38. }
  39.  
  40.  
  41. int main()
  42. {
  43. ios::sync_with_stdio(0);
  44. #ifdef _DEBUG
  45. freopen("input.txt", "r", stdin);
  46. freopen("output.txt", "w", stdout);
  47. #endif
  48.  
  49. int t; cin>>t;
  50.  
  51. while(t--){
  52. memset(vis,-1,sizeof vis);
  53. cin>>n;
  54. for(int i=1; i<=n; i++) cin>>a[i];
  55. ok = 0; cin>>m; fun(1,0);
  56. if( ok ) cout<<"YES"<<endl;
  57. else cout<<"NO"<<endl;
  58. }
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement