Thursday, 15 October 2015

Rupsa and Equilateral Triangle (x^2=y^2-z^2)

Rupsa and Equilateral Triangle 


Rupsa really loves triangles. One day she came across an equilateral triangle having length of each side as an integer N. She started wondering if it was possible to transform the triangle keeping two sides fixed and alter the third side such that it still remains a triangle, but the altered side will have its length as an even integer, and the line drawn from the opposite vertex to the mid-point of the altered side is of integral length.
Since Rupsa is in a hurry to record a song for Chef as he really loves her songs, you must help her solve the problem as fast as possible.

Input

The first line of input contains an integer T denoting the number of test cases.
Each test-case contains a single integer N.

Output

For each test case, output "YES" if the triangle transformation is possible, otherwise "NO" (quotes for clarity only, do not output).

Constraints

  • 1 ≤ T ≤ 106
  • 1 ≤ N ≤ 5 x 106

Sub tasks

  • Subtask #1: 1 ≤ T ≤ 100, 1 ≤ N ≤ 104 (10 points)
  • Subtask #2: 1 ≤ T ≤ 104, 1 ≤ N ≤ 106 (30 points)
  • Subtask #3: Original Constraints (60 points)

Example

Input:
2
5
3

Output:
YES
NO

Explanation

  • In test case 1, make the length of any one side 6, and it will suffice.
-----------------------------------editorial---------------------------------------------------------------------------------------------------

Pre-requisites

Basic math

Problem

Given an equilateral triangle having length of each side as an integer N. Is it possible to alter one of its' sides such that the height drawn to the altered side has integral length and the altered side has an even integer length?

Explanation

Let's recall the formula for calculating of length of the height of an isosceles triangle. If the length of the height is h, the length of the altered side is b, then h = sqrt(N2-b2/4). Then, h2=N2-b2/4. Let's denote b/2 by c and, since bshould be even, c should be integer. Then, we get h2=N2-c2. This is the same as N2=h2+c2. According to the statement, h and c should be integer.
Now, the problem is: given an integer N. Find out, whether it is possible to represent N2 as a sum of two integer numbers' squares or not.

How to get 10 points

Let's iterate through possible values of h, there are O(N) possible values of h. Then, let's check, if N2-h2 is a square of an integer number.
The total complexity of such a solution is O(TN) and it is capable only of solving the first subtask.

How to get 100 points

It is a well known fact that N can be represented as a sum of two perfect squares if and only if N is dividible by a prime of the form (4k+1), where k is an integer number.
So, let's find all the prime numbers not exceeding the maximal value of N. Then, let's pick all the prime numbers that give 1 modulo 4 and for each of them, mark all the numbers that are divisible by them and doesn't exceed the maximal possible value of N (say, MAX_N).
It takes MAX_N/P operations to mark all the integers that are divisible by P. Let's estimate the total number of the operations that we will make.
Since not every number is a prime of the form (4k+1), we can safely assume that the total number of operations won't exceed NP=1NP that won't exceed O(T+MAX_N log MAX_N).
This solution gets 100 points.

---------------------------------------------------code------------------------------------------------------------
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int lli;
  4.  
  5. #define test() int test_case;cin>>test_case;while(test_case--)
  6. #define fr(i,n) for(int i=0;i<n;i++)
  7. #define frr(i,a,n) for(int i=a;i<n;i++)
  8. #define sll(a) scanf("%lld",&a)
  9. #define sl(a) scanf("%ld",&a)
  10. #define si(a) scanf("%i",&a)
  11. #define sd(a) scanf("%ld",&a)
  12. #define sf(a) scanf("%f",&a)
  13. #define rn(a) return a
  14. #define pai pair<int,int>
  15. #define pal pair<li,li>
  16. #define pall pair<lli,lli>
  17. #define ff first
  18. #define ss second
  19. #define mod 1000000007
  20. #define mp make_pair
  21. #define pb push_back
  22. #define pll(a) printf("%lld\n",a)
  23. #define pl(a) printf("%lld\n",a)
  24. #define pi(a) printf("%d\n",a)
  25. #define pd(a) printf("%lf\n",a)
  26. #define pf(a) printf("%f\n",a)
  27. #define lc (start+end)/2
  28. #define rc lc+1
  29. int sval;
  30. bool pcheck[9000000];
  31. bool num[9000000];
  32. int main()
  33. {
  34. pcheck[2]=0;
  35. int maxi=6000000;
  36. for(int i=2;i<=maxi;i++)
  37. {
  38. if(!pcheck[i])
  39. {
  40. bool checker=false;
  41. if((i-1)%4==0)
  42. {
  43. checker=true;
  44. num[i]=1;
  45. }
  46. for(int j=2;i*j<=9000000;j++)
  47. {
  48. pcheck[i*j]=1;
  49. //cout<<i*j<<" ";
  50. num[i*j]=checker | num[i*j];
  51. }
  52. }
  53. }
  54. test()
  55. {
  56. int n;
  57. scanf("%d",&n);
  58. if(n==1)
  59. printf("NO\n");
  60. else if(num[n])
  61. {
  62. printf("YES\n");
  63. }
  64. else
  65. printf("NO\n");
  66. }
  67. return 0;
  68. }

No comments:

Post a Comment