Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. int primap(int a[],int n,int st,int x)
  5. {
  6. int s=st,d=n,poz=-1,m;
  7. if(x<a[s] or x>a[d])
  8. return -1;
  9. while(s<=d)
  10. {
  11. m=(s+d)/2;
  12. if(a[m]==x)
  13. {
  14. poz=m;
  15. d=m-1;
  16. }
  17. else if(a[m]>x)
  18. d=m-1;
  19. else s=m+1;
  20. }
  21. return poz;
  22. }
  23. int ultimaap(int a[],int n,int st,int x)
  24. {
  25. int s=st,d=n,poz=-1,m;
  26. if(x<a[s] or x>a[d])
  27. return -1;
  28. while(s<=d)
  29. {
  30. m=(s+d)/2;
  31. if(a[m]==x)
  32. {
  33. poz=m;
  34. s=m+1;
  35. }
  36. else if(a[m]>x)
  37. d=m-1;
  38. else s=m+1;
  39. }
  40. return poz;
  41. }
  42. int main()
  43. {
  44. int n,i,a[100001],nrsol=0,uap,pap,x,p2[33],j;
  45. cin>>n;
  46. for(i=1; i<=n; i++)
  47. cin>>a[i];
  48. sort(a+1,a+1+n);
  49. p2[0]=1;
  50. for(i=1; i<=30; i++)
  51. p2[i]=p2[i-1]*2;
  52. for(i=1; i<n; i++)
  53. {
  54. for(j=1; j<=30; j++)
  55. {
  56. x=p2[j]-a[i];
  57. if(x>=a[1] and x<=a[n])
  58. {
  59. pap=primap(a,n,i+1,x);
  60. if(pap>-1)
  61. {
  62. uap=ultimaap(a,n,i+1,x);
  63. nrsol+=(uap-pap+1);
  64. }
  65. }
  66. }
  67. }
  68. cout<<nrsol;
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement