Common BoardHow to solve this problem in 0.5 sec? I have used simple brute force algo - qsort strings every time I needed it, and have used strings hashes for comparison. I got AC in 3,2 sec Straightforward N*log(N) solution is building AVL or R/B tree of strings. But a simpler implementation is sort all input +xxxxx strings (along with "sun") and replace AVL tree with interval tree on which string indices have already been added. Edited by author 17.08.2008 23:47 Another solution is building characters tree, so that paths to terminal nodes correspond to existing strings. First-child & next-sibling indexation will suit memory limit. I also accepted it using AVL tree, but first i tried characters tree. IMHO this solution can't pass memory limit because of many pointers in tree. C++ optimizations haven't helped me. I get 0.125 with not optimal solution, admins, change 10^4 to 10^5, it is cool problem.... First i tried to use character tree - MLE. Then i tried to use character tree with pointers "firstChild" and "nextSibling" in nodes - MLE. Then i realized that i can try to try forcing GC in Java, but it TLE. After all, just for fun, tried with TreeSet and own MyString class... AC 3.6 o_O Just wonder, how people solve this task on Java in 0.2s and 2MB. It seems like they use plain arrays of chars 20*100000. I use sqrt decomposition and get 0.093 sec Edited by author 26.10.2013 12:03 Edited by author 26.10.2013 12:03 #include <cstdio> #include <cmath> Этот код не работает g++ (дает wa на 1й тест), у мне 4.6.2 - ответ теста совпадает. Но этот код рабочий с VS++2000 Дайте вывод вашего компилятора (вывод приложения после вашего компилятор) int main() { long double *n = new long double[10000000]; long double buf;25 int l=0; while(std::scanf("%Lf",&buf) != EOF) n[l++]= std::sqrt(buf); l-=1; while (l>=0) std::printf("%.4Lf\n",n[l--]); } его прекрасно работать в шахте g+ + 4.6.2 WA.. who can give some test cases?
#include<stdio.h> #include<stdlib.h> int main() { long long n,m; scanf("%I64d",&n); m=n*(n-3)/2; if (n % 2==1) printf("%I64d 0\n",m); if (n % 2==0) { if (n==4)printf("0 2\n"); else printf("%I64d %I64d\n",m,m); } system("pause"); return 0; } Edited by author 20.10.2013 09:53 n=4 n=5 n=6 I think that number of perpendicular diagonals for N-polygon is equal to 0 for every odd N. I don't say the same about parallel diagonals in regular N-polygon for odd N. Edited by author 24.10.2013 19:33 answers for tests: n = 4 ans: 0 2 n = 5 ans: 0 0 n = 6 ans: 6 9 n = 7 ans: 14 0 Use long long instead int I think, in some tastes circles are not "ideal" (i. e. absolutely symmetric with respect to both axes and its' centre) +1. The problem is elegant, but some tests are messy. Test 2: "circle 50x56" is oval. what is test 8? Edited by author 25.10.2013 07:44 #include<iostream> using namespace std; int main() { double p,q; cin>>p; cin.get(); cin>>q; long tmp_q = 1,tmp_p = 0; for(int i = 1;i < size_t(-1); ++i) { tmp_p = p * i * 100; tmp_q = q * i * 100; long res = (int)(tmp_p/10000); ++res; if(tmp_q % 10000 == 0) { if(res < (int)(tmp_q/10000)) { cout<<i<<endl; break; } } else { if(res <= (int)(tmp_q/10000)) { cout<<i<<endl; break; } } } } some hints?? where's the problem??? What is the output for this two inputs?? 3 2 1 2 and 3 3 1 2 2 Two very helpful tests: test: 3 2 1 2 ans:NO test: 3 3 1 2 2 ans:NO if this test 3 3 1 2 3 ans:YES ??? My AC solution outputs extra empty line before outputing answer for the task. I think, it is not right, or I'm mistaken? That is OK. In this problem you may output any space characters before and after password. Interesting, that my solution got AC after I added \n to the end. Without \n (just 16 symbols) I have WA#1. Problems with checker? the last salary may be less than what we thought. input: 100 16 output: 10 if the last salary were 100, he got 9 jobs in all. but in this case, the last salary is not 100. it's 99. So he has got 10 jobs. try this and that's all clear input: 99 16 output: 10 Edited by author 07.05.2011 20:39 tu n-ai ce face? stai cred ca stiu raspunsul....nu tu n-ai ce face? stai cred ca stiu raspunsul....nu, de ce? give us the mode you arrive from 16 to 100 in 10 increases, if possible and sorry for spam my friend is idiot(alex t) Edited by author 24.10.2013 20:10 Use the same logic as in Unit1012 (do not make recursive calls, instead use a register of 3 values, f(N-1), f(N-2) and the one you are calculating: f(N). Then calculate all values from i=2 to N and use register. This reduces memory requirements :), makes the code for this problem faster.) Who can help me. I use big int class in c++. Edited by author 15.08.2010 19:36 maybe your long arithmetics works too slow. what base are you using when performing adding/multiplying? what about algo? Edited by author 15.08.2010 20:20 Edited by author 16.08.2010 12:46 // 1012.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <deque> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; const int SZ = 1000; class verylong { private: deque<char>vlstr; int vlen; verylong multdigit(const int) const; verylong mult10(const verylong) const; public: verylong() : vlen(0) { vlstr[0]='\0'; } verylong(const char s[SZ]) { strcpy(vlstr, s); vlen=strlen(s); } verylong(const unsigned long n) { sprintf(vlstr, "%lu", n); _strrev(vlstr); vlen=strlen(vlstr); } void putvl() const; void getvl(); verylong operator + (const verylong); verylong operator * (const verylong); verylong operator - (const verylong); }; //-------------------------------------------------------------- void verylong::putvl() const { char temp[SZ]; strcpy(temp,vlstr); cout << _strrev(temp); } //-------------------------------------------------------------- void verylong::getvl() { cin >> vlstr; vlen = strlen(vlstr); _strrev(vlstr); } //-------------------------------------------------------------- verylong verylong::operator + (const verylong v) { char temp[SZ]; int j; int maxlen = (vlen > v.vlen) ? vlen : v.vlen; int carry = 0; for(j = 0; j<maxlen; j++) { int d1 = (j > vlen-1) ? 0 : vlstr[j]-'0'; int d2 = (j > v.vlen-1) ? 0 : v.vlstr[j]-'0'; int digitsum = d1 + d2 + carry; if( digitsum >= 10 ) { digitsum -= 10; carry=1; } else carry = 0; temp[j] = digitsum+'0'; } if(carry==1) temp[j++] = '1'; temp[j] = '\0'; return verylong(temp); } //-------------------------------------------------------------- verylong verylong::operator - (const verylong v) { char temp[SZ]; int j, p=0, r=0; int maxlen = (vlen > v.vlen) ? vlen : v.vlen; p = maxlen - 1; int carry = 0; for(j=0; j<maxlen; j++) { int d1 = (j > vlen-1) ? 0 : vlstr[j] - '0'; int d2 = (j > v.vlen - 1) ? 0 : v.vlstr[j] - '0'; int digitdif = d1 - d2 - carry; if(digitdif < 0) { digitdif += 10; carry = 1; } else carry = 0; temp[j] = digitdif + '0'; } temp[j] = '\0';
return verylong(temp); } //-------------------------------------------------------------- verylong verylong::operator * (const verylong v) { verylong pprod; verylong tempsum; for(int j=0; j<v.vlen; j++) { int digit = v.vlstr[j]-'0'; pprod = multdigit(digit); for(int k=0; k<j; k++) pprod = mult10(pprod); tempsum = tempsum + pprod; } return tempsum; } //-------------------------------------------------------------- verylong verylong::mult10(const verylong v) const { char temp[SZ]; for(int j=v.vlen-1; j>=0; j--) temp[j+1] = v.vlstr[j]; temp[0] = '0'; temp[v.vlen+1] = '\0'; return verylong(temp); } //-------------------------------------------------------------- verylong verylong::multdigit(const int d2) const { char temp[SZ]; int j, carry = 0; for(j = 0; j<vlen; j++) { int d1 = vlstr[j]-'0'; int digitprod = d1 * d2; digitprod += carry; if( digitprod >= 10 ) { carry = digitprod/10; digitprod -= carry*10; } else carry = 0; temp[j] = digitprod+'0'; } if(carry != 0) temp[j++] = carry+'0'; temp[j] = '\0'; return verylong(temp); } int main() { verylong a[180], s[180]; unsigned long n, i, k; scanf("%d", &n); scanf("%d", &k); a[0] = (n-n); s[0] = (k-1);
for(i=1; i<n; i++) { s[i] = (verylong)k * s[i-1]; s[i] = s[i] - a[i-1]; a[i] = s[i-1] - a[i-1]; }
s[n-1].putvl(); return 0; } waiting.... // 1012.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <deque> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; const int SZ = 1000; class verylong { private: deque<char>vlstr; int vlen; verylong multdigit(const int) const; verylong mult10(const verylong) const; public: verylong() : vlen(0) { vlstr[0]='\0'; } verylong(const char s[SZ]) { strcpy(vlstr, s); vlen=strlen(s); } verylong(const unsigned long n) { sprintf(vlstr, "%lu", n); _strrev(vlstr); vlen=strlen(vlstr); } void putvl() const; void getvl(); verylong operator + (const verylong); verylong operator * (const verylong); verylong operator - (const verylong); }; //-------------------------------------------------------------- void verylong::putvl() const { char temp[SZ]; strcpy(temp,vlstr); cout << _strrev(temp); } //-------------------------------------------------------------- void verylong::getvl() { cin >> vlstr; vlen = strlen(vlstr); _strrev(vlstr); } //-------------------------------------------------------------- verylong verylong::operator + (const verylong v) { char temp[SZ]; int j; int maxlen = (vlen > v.vlen) ? vlen : v.vlen; int carry = 0; for(j = 0; j<maxlen; j++) { int d1 = (j > vlen-1) ? 0 : vlstr[j]-'0'; int d2 = (j > v.vlen-1) ? 0 : v.vlstr[j]-'0'; int digitsum = d1 + d2 + carry; if( digitsum >= 10 ) { digitsum -= 10; carry=1; } else carry = 0; temp[j] = digitsum+'0'; } if(carry==1) temp[j++] = '1'; temp[j] = '\0'; return verylong(temp); } //-------------------------------------------------------------- verylong verylong::operator - (const verylong v) { char temp[SZ]; int j, p=0, r=0; int maxlen = (vlen > v.vlen) ? vlen : v.vlen; p = maxlen - 1; int carry = 0; for(j=0; j<maxlen; j++) { int d1 = (j > vlen-1) ? 0 : vlstr[j] - '0'; int d2 = (j > v.vlen - 1) ? 0 : v.vlstr[j] - '0'; int digitdif = d1 - d2 - carry; if(digitdif < 0) { digitdif += 10; carry = 1; } else carry = 0; temp[j] = digitdif + '0'; } temp[j] = '\0';
return verylong(temp); } //-------------------------------------------------------------- verylong verylong::operator * (const verylong v) { verylong pprod; verylong tempsum; for(int j=0; j<v.vlen; j++) { int digit = v.vlstr[j]-'0'; pprod = multdigit(digit); for(int k=0; k<j; k++) pprod = mult10(pprod); tempsum = tempsum + pprod; } return tempsum; } //-------------------------------------------------------------- verylong verylong::mult10(const verylong v) const { char temp[SZ]; for(int j=v.vlen-1; j>=0; j--) temp[j+1] = v.vlstr[j]; temp[0] = '0'; temp[v.vlen+1] = '\0'; return verylong(temp); } //-------------------------------------------------------------- verylong verylong::multdigit(const int d2) const { char temp[SZ]; int j, carry = 0; for(j = 0; j<vlen; j++) { int d1 = vlstr[j]-'0'; int digitprod = d1 * d2; digitprod += carry; if( digitprod >= 10 ) { carry = digitprod/10; digitprod -= carry*10; } else carry = 0; temp[j] = digitprod+'0'; } if(carry != 0) temp[j++] = carry+'0'; temp[j] = '\0'; return verylong(temp); } int main() { verylong a[180], s[180]; unsigned long n, i, k; scanf("%d", &n); scanf("%d", &k); a[0] = (n-n); s[0] = (k-1);
for(i=1; i<n; i++) { s[i] = (verylong)k * s[i-1]; s[i] = s[i] - a[i-1]; a[i] = s[i-1] - a[i-1]; }
s[n-1].putvl(); return 0; } Here is my simple program ////////// #include<iostream> using namespace std; int main() { int a[2000]={0},b[2000]={0},c[2000]={0}; int g,n,k,i,j,m=1,t=2000; cin>>n>>k; a[t-1]=1; b[t-1]=k-1; for(i=2;i<=n;i++) { g=0; for(j=t-1;j>=t-m;j--) { c[j]=a[j]+b[j]; c[j]=c[j]*(k-1)+g; if (c[j]>9) {g=c[j]/10; c[j]=c[j]%10; }else g=0; a[j]=b[j]; b[j]=c[j]; } if (g!=0) { m++; b[t-m]=g; c[t-m]=g; } } for(j=t-m;j<t;j++) cout<<c[j]; } Hi, I think the major problem is operator *. You seem to use vlen of verylong objects store the intermediate value. That's not a good idea, as 10-based long algorithm already has the risk of TLE, let alone that staff. Try not to use multy10 but to store intermediate value in a char array instead. I can send you my code (in C). btw, VC2010 compiler says it doesn't allow using deque as an argument of strxxx. How did you pass compilation with this code? You don't necessarily use *. + is enough. And quicker. I've tested. It works in this way very fast. Remember to enlarge array. Edited by author 19.08.2010 13:36 Edited by author 19.08.2010 13:46 My int main algo is right or not? I wrote it in VC 2010. You must wipe out #include "stdafx.h" and main function must be int main() Edited by author 19.08.2010 14:10 my mail: johnterryr@mail.ru I think algo all right. But it won't compile if using deque. After changing it into char[5000] it works, producing the same answer for 1800,10 as mine. However, for input 10,2 the output is 089, I don't know why. I'll send you my program later this evening. yes i wanted change answers like that into 89 , But i sent it and AC.And i didn't take care of it after AC, Sent. See email for detail. Do not make recursive calls, instead use a register of 3 values, f(N-1), f(N-2) and the one you are calculating: f(N). Then calculate all values from i=2 to N and use register. This reduces memory requirements :), makes the code for this problem faster. Use Brute Force approach with Lagrange's four square theorem, but to short-cut do check only 1, 2, 3 case. If those do not give any squares then output 4 (see Lagrange's theorem). :) program ex; var a,b:array[1..1000000] of int64; i,t,n,m,c,k:int64; begin readln(n); For i:=1 to n do read(a[i]); readln(m); for i:=1 to m do read(b[i]); c:=0; for i:=1 to n do begin k:=0; for t:=1 to m do if a[i]=b[t] then k:=1; if k=1 then c:=c+k; end; write(c); end. "There were 4 errors compiling module, stopping" Of course, Dynamic Programming solution is better. But for your info there is a theorem of Lagrange. Quote from Wikipedia "Lagrange's four-square theorem states that any natural number can be represented as the sum of four integer squares" http://en.wikipedia.org/wiki/Lagrange%27s_four-square_theorem Example, 3 = 1^2 + 1^2 + 1^2 + 0^2 31 = 5^2 + 2^2 + 1^2 + 1^2 310 = 17^2 + 4^2 + 2^2 + 1^2. Here is my C# code: using System; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); for (int i1 = 0; i1 <= (int)Math.Sqrt(n); i1++) for (int i2 = 0; i2 <= (int)Math.Sqrt(n); i2++) for (int i3 = 0; i3 <= (int)Math.Sqrt(n); i3++) for (int i4 = 0; i4 <= (int)Math.Sqrt(n); i4++) if (i1 * i1 + i2 * i2 + i3 * i3 + i4 * i4 == n) { Console.WriteLine(Math.Sign(i1)+Math.Sign(i2)+Math.Sign(i3)+Math.Sign(i4)); return; } } } up So? any answers from admins? Please help me....... Thanks in advance It's similar to knapchak problem Even knapsack passes the tests. 1) Use reccurent formula, but do no recursion, and 2) Use BigInteger for the result Good luck:) 3 arrays [8100*900] of byte O<8100*900*9, DP Edited by author 10.09.2009 18:56 Only one with 0.25 time and 7K memory. Isnt your time complexity O(900*8100*9)? Why when I allocated staticly globaly memory like this char a[10005][10005] = {0}; char d[10005][10005] = {0}; I got MLE 4, but not MLE 1? You can used only: char a[901][8101]; char d[901][8101]; Because 9....9 (100 times) S = 900 and S^2 = 8100 Maybe because of the compiler's optimizations |
|