|
|
The probable cause of the error is the use of a floating point variable for computing of value a. Any floating point types have limited precision, and looks like some times it is not enough. Consider to use some integer types instead. please answer to me. 6 years have passed. But anyway, maybe this will help someone, the test number 3 is N = 1 5 years passed. anyway thanks. I has helped me. :) for (long double N = 1; N < sqrt(2*S)+3; N++) { } Bruteforse gets TL on Python but works on c++ Try: 2 Correct answer is 2 1 My AC solution returns 55555556 2 My answer is 61313 68558 Hi, Mine is same as Howard Liu i.e. 55555556 2 Varun > My answer is 61313 68558 This is an answer for 6553562057. I also get 55555556 2 for 111111113. I used some methods, got TLE || WA ...T T ... You can enumerate P . Think carefully and you will get a O(sqrt(n)) solution. Spend some time for enum, cause i don't wanna to calculate in float variables. So fun!) If you have WA 2 look this test 6 ans 1 3 10 ans 1 4 my programm gave for 6 ans 0 4 for 10 0 5 after changes about a1 = 0 AC.So a1 Not equal 0 It must be added to the statement. It IS in the statement: "Your program is to output two POSITIVE integers A and P separated with a space..." N = int(input()) if N == 1: print(1,1) exit() for P in range(round(N**0.5)+1,0,-1): A = (2*N - P**2 + P) / (2*P) if A % 1 == 0: print(int(A),P) exit() Don't forget that not only P should be divider of 2N but P+2A-1 should be divider of 2N too. It was my mistake when I got WA 7 #include<iostream> #include<math.h> using namespace std; int main() { int n; int p; double a; int i; cin>>n; int flag; for(p=sqrt(n*2);p>0;p--) { a=(double)n/p-(double)(p-1)/2; if(a==(int)a && a>0) { cout<<a<<" "<<p; return 0; }
} cout<<n<<" "<<1; return 0; } Edited by author 09.10.2008 14:39 cout << (int)a << " " << p; :) I've got quadratic equation. It can be calculated if you know just what is arithmetical progression. What I should to know in maths to find max P (I found P from the equation). Edited by author 29.04.2016 01:05 #include <iostream> #include <math.h> using namespace std; long long i, n, d; double a; int p; int main(){ cin >> n; n *= 2; p =(int) sqrt(n + 0.0); while(1){ a = (n - p * p + p + 0.0) / (2 * p); if(0 < a && a == (int) a && (p * p + 2 * a * p - p) == n) break; p --; } cout << (int)a << ' ' << p; } #include<iostream> #include<math.h> using namespace std; int main() { long long n,p,a=1; cin>>n; while(1) { p=(1-2*a+sqrt((2*a-1)*(2*a-1)+8*n))/2; if(2*n==2*p*a+p*(p-1)) { cout<<a<<" "<<p; break; } a++; } return 0; } On test #9 my solution got the same problem. Edited by author 14.11.2015 14:30 What is in test #7 ?? Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; var n = int.Parse(Console.ReadLine().Trim()); int a = 0; var p = (int)Math.Truncate(Math.Sqrt(n + n)); while (p > 1) { var s = n - p * (p + 1) / 2; if (s >= 0) { int rem; a = Math.DivRem(s, p, out rem); if (rem == 0) break; } p--; } a++; Console.WriteLine("{0} {1}", a, p); I got it! [code deleted] Edited by moderator 21.10.2019 23:03 O(sqrt(2 * N)) I have: A = (2 * N - P * P + P ) / ( 2 * P ) and P = [1, 2, 3, ..., sqrt(2 * N) + 3] :D I got AC ... Sorry for russian comments, I don't speak English well. Why WA? (system("pause"); is only for checking, of course) #include <iostream> using namespace std; int main() { unsigned long N, P, A; cin >> N; for (A = 1; A <= N; A++) // чтобы P было максимальным, начинаем с меньшего А { // прокручиваем P с N/A вниз, т.к. вроде бы это минимальное число с которого следует начинать for (P = N / A; P*A + P*(P - 1) / 2 > N; P--); if (P*A + P*(P - 1) / 2 == N) // если нашли - останавливаемся break; } cout << A << ' ' << P; system("pause"); return 0; } Edited by author 24.02.2015 13:16 #include <cmath> #include <cstdio> #include <iostream> using namespace std; typedef long long LL; int main() { LL N,A,P,up; cin>>N; up=(LL)ceil((1.00+sqrt(1.00+8.00*(double)N))/2.00); P=up; while (((2*N)%P)!=0) { P--; }
A=(((2*N)/P)-P+1)/2;
cout<<A<<" "<<P<<endl; } This is my code...but this is giving WA on #2. I can't understand the reason. It seems to give correct answer on the tests I do on my computer..?!
At first sight, I strongly suggest you to avoid operating repeteadly with floating points, the little errors will accummulate, and WA comes to the order. at second sight the logic is just not right. Look, I got the same algorithm that a dude with TLE at test9, but with a less complex break condition. private static int sq(long A,long N){ double aux= Math.sqrt( ((A*A)<<2) - (A<<2) + 1 + 8*N); int r = (int)aux; if ((double)r==aux) return r; else return -1; } public static void main (String[]args){ Scanner s=new Scanner(System.in); int N= s.nextInt(); long A=1,P=1,root=0; while (true){ root= sq(A,N); if (root==-1) A++; else{ P= (1-(A<<1)+root)/2; break; } } System.out.println(A+" "+(P)); } Я один раскладывал 2*n на всевозможные множители?) |
|
|