Общий форумI used such variables: map<string,int> id; set<string> list[1003]; map<string,int> mp; vector<string> res; but got TL test 10. Can i get AC with same solution? Yes if instead string use struct{ int num; int row; int poz1; int poz2; }; to have deal with segments of char S[1000][125] to pass test 10 you must do 1) compare hash codes instead of string compare 2) use vectors+sort. In this case vector is way faster than std::set 3) fast I/O. 4) fast list intersection good luck! If you have WA3 try this: 1 3 5 1 4 5 7 Right answer is 1 If you have WA6 try to change sum<k to sum<=k. It helped me. Edited by author 28.01.2014 20:34 There *is* a period of 9973, but not for the given function. Let's denote 9973 as M. ////////////////////////////////////////////////////////////////////////// //In short//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// 1. g(n) is linear by f(n - 1), and g(x, y) = g(x mod M, y), which means we can find such A and B (in O(M)) that f(x + M) = (A * f(x) + B) mod M. 2. Having found such A and B, it's easy to show that for p > 0, f(p * M) = B * (A^(p-1) + A^(p-2) + .. + A^2 + A + 1) mod M. I'm not sure if the last sum can be calculated in O(1), but it surely can be found in O(M). 3. So, we calc A and B, read N, find f(N - N % M) and then iterate till N in O(M), resulting with overall O(M). ////////////////////////////////////////////////////////////////////////// //In detail/////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// ////// //1.// ////// Let's assume we know f(i) and wish to find f(i+1). Easy to see, g(x, y) = g(x mod M, y). This is why, f(i + 1) = g(i + 1, f(i)) = g((i + 1) mod M, f(i)). Let's denote (i + 1) mod M as j. f(i + 1) = g(j, f(i)) = ((f(i) - 1) * j^5 + j^3 – j * f(i) + 3 * j + 7 * f(i)) mod M f(i + 1) = f(i) * (j^5 - j + 7) + (-j^5 + j^3 + 3 * j) mod M f(i + 1) = f(i) * ((j^5 - j + 7) mod M) + (-j^5 + j^3 + 3 * j) mod M Remembering that j = (i + 1) mod M, let us denote U(i) = (j^5 - j + 7) mod M, V(i) = (-j^5 + j^3 + 3 * j) mod M. As a result: f(i + 1) = U(i) * f(i) + V(i) Note that for any i, we calculate U(i) and V(i) in O(1) time. Also note that U(i) = U(i mod M) and V(i) = V(i mod M): f(i + 1) = U(i mod M) * f(i) + V(i mod M) Let's consider some certain x, and let's denote f(x) as X. f(x+0) == X == 1 * X + 0 == (a0 * X + b0) (mod M) f(x+1) == U(0) * f(x+0) + V(0) == (a1 * X + b1) (mod M) f(x+2) == U(1) * f(x+1) + V(1) == (U(1) * a1) * X + (U(1) * b1 + V(1)) == (a2 * X + b2) (mod M) f(x+3) == U(2) * f(x+2) + V(2) == (U(2) * a2) * X + (U(2) * b2 + V(2)) == (a3 * X + b3) (mod M) f(x+4) == U(3) * f(x+3) + V(3) == (U(3) * a3) * X + (U(3) * b3 + V(3)) == (a4 * X + b4) (mod M) ... f(x+M) == (aM * X + bM)) (mod M) Note that aM and bM do not depend on x - this is the key point! Let's denote aM as A and bM as B. Each iteration above needs O(1) time, so we found A and B in O(M), such that for any x, f(x+M) = (A * f(x) + B) mod M ////// //2.// ////// Consider the following sequence f(0 * M) == 0 (mod M) f(1 * M) == A * f(0 * M) + B == B (mod M) f(2 * M) == A * f(1 * M) + B == A * B + B == B * (A + 1) (mod M) f(3 * M) == A * f(2 * M) + B == A * B * (A + 1) + B == B * (A^2 + A + 1) (mod M) ... f(p * M) == B * (A^(p-1) + A^(p-2) + .. + 1) (mod M) Function A^p mod M is periodic by p with period being divisor of M, so we can easily calculate f(p * M) in O(M) time by summing up first M items of the sequence above. Moreover, for this particular problem p (see below) is very small - less than N/M, so we may calculate this sum explicitly. ////// //3.// ////// The most pleasant part. Given N, we set p to N / M, and calculate f(p * M) with the method desribed above. N - p * M < M, so all we have to do left is explicitly apply the initial formula N - p * M times to find f(N). Edited by author 15.02.2006 18:23 that is the best solution i've ever seen ,thank you Can you explain why did you decide that the coefficients aM and bM don't depend on x? That's right I guess, but it's not evident for me. Why g(x,y) = g(x mod M,y) Correct me if I am mistaken but I think this is because U(i) = U(i mod M) and V(i) = V(i mod M). Please give me some more tests. And what is the test number 4? Please, give me some hints and idea. I write bruteForce but i see no hints. Only random 0 and 1. Length of the answer is 2^n + n - 1, it can be constructed with brute-force Thank you. Please, give me more hints. =) Above hint is more than enough to solve it. The only possible latest hint: during BF, when construct a sequence, think how to check in O(1) that the new suffix of length n didn't occur in the sequence before. #include<iostream> using namespace std; int main() { int a,b; cin>>a>>b; cout<<a+b; return 0; } after int main() need "{" #include <fstream> using namespace std; int main() { ifstream fin("INPUT.TXT"); ofstream fout("OUTPUT.TXT"); int a,b; fin >> a >> b; fout << a + b; fin.close(); fout.close(); } поидет ?? Мен агылшынша тiлде белмимын!! How to respond to the tasks in your site? a file or command line? tell me if the command line .. Read FAQ Кай жерде ' read' ? Корсетше мағам .. namespace Project1 { class Programa { static void Main() { Console.Write("Введите первое число: "); int First = Convert.ToInt32(Console.ReadLine()); Console.Write("Введите второе число: "); int Second = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Сумма числа {0} и числа {1} равна {2}", First, Second, (First + Second)); Console.ReadKey(); } } } Edited by author 18.01.2013 23:09 Edited by author 18.01.2013 23:09 or namespace Project2 { class adsf { static void Main() {
int x; int y;
x = Convert.ToInt32(Console.ReadLine()); y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Сумма числел = " + (x+y));
Console.ReadKey(); } } } #include <iostream> int main() { using namespace std; int a, b, buffer; cin>>a>>b; int *mass=new int[b]; int *deep=new int[33180]; for (int i4=0; i4<b; i4++) { mass[i4]=0; deep[i4]=1200; } for (int i5=b; i5<33180; i5++) { deep[i5]=a; } for (int i6=0; i6<33180; i6++) { for (int i9=1; i9<33180; i9++) { if (deep[i9-1]>deep[i9]) { buffer=deep[i9-1]; deep[i9-1]=deep[i9]; deep[i9]=buffer; } } } mass[0]=a; mass[b-1]=b; for (int i=0; i<b; i++) { for (int i2=1; i2<b; i2++) { if (mass[i2-1]>mass[i2]) { buffer=mass[i2-1]; mass[i2-1]=mass[i2]; mass[i2]=buffer; } } } int sum=0; for (int i3=0; i3<b; i3++) { sum+=mass[i3]; } cout<<sum; delete []deep; delete []mass; return 0; } This solution works 1 second and output 1 if a=1 or b=1, and get AC! ab = raw_input() print int(ab[0]) + int(ab[2]) Yup, I have implemented gradient descent with time cheking only to find it fail on tests 4,5 and then (after some constants manipulations) on test 9, and then just read a forum reply and implemented that "dumb" solution. Edited by author 28.01.2014 02:39 You can use "random" function to produce a letter. I got curious and tested the "random" solution for 100000 times, and 88058 times (which is about 88%) it worked correctly. I've got WA#11. Here is my algorithm: For each row, column and two diagonals count number of 'X' and 'O'. - If in any case number of 'X' is 2 and # of 'O' is zero, then crosses win (coz it's crosses turn). - If # of 'X' is 1 and # of 'O' is 0, then increase counter for X wins (if it's larger than 1 Crosses win). - If # of 'O' is 2 and # of 'X' is 0, then increase counter for O wins (if it's larger than 1 Ouths win).
Then I'll check if counter for X wins is larger than 1, print crosses win - else if counter for O wins is larger than 1, print Ouths win - else Draw We can just rotate our second diamond and compare it to the first diamond. Like this: for(int i=0;i<4;i++) {for(int j=0;j<3;j++) {if(a==b) {cout<<"equal"<<endl;return 0;} // Rotate diamond around current base-face string x=e+b[2]+b[3]+b[1]; b=b[0]+x;} string t="abcd"; // Change base-face if(i!=2) {t[0]=b[1];t[1]=b[3]; t[2]=b[2];t[3]=b[0];} else {t[0]=b[2];t[1]=b[1]; t[2]=b[3];t[3]=b[0];} b=t;} I have got AC. For test: 1 401 64481201 my AC program answers 2, when the correct answer is 3. Student can drink 3 mugs of ale: 401-160801-64481201 Add one more test: 20 1 536870912 1 536870912 1 214358881 1 147008443 1 352275361 1 28934443 1 214921799 1 1042441 1 2627641 1 6086089 1 12823561 1 24730729 1 44930209 1 77810041 1 129254161 1 207561649 1 319730161 1 480091921 1 707081281 1 31973 Correct answer: 30 30 9 6 5 4 4 3 3 3 3 3 3 3 3 3 3 3 3 2 My Ac program answers: 30 30 9 6 5 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 Your test is added. If you have more tests, please, send them to timus_support@acm.timus.ru. I don't have any more tests. Edited by author 27.01.2014 18:43 Since k <= 10 and |ai| ≤ 1000 the largest answer is 10001: let use -11000..11000 to be sure. Since the end of each segment is an integer such that on one side inequality holds and on the other it is not, the solution is trivial: just find all such points. Still the following program gives WA4. Any hint? #define I(x) int x; cin >> x #define FA(i,c) for(auto& i: c) #define FE(i,a,b) for(int i = (a); i <= (b); ++i) typedef vector<int> V; V a; bool in(double x){ FA(ai,a) x = fabs(x-ai); return x < 1; } int main(){ I(k); a.resize(k); FA(ai,a) cin >> ai; V b; FE(ix,-11000,11000) if(in(ix-0.1) != in(ix+0.1)) b.push_back(ix); cout << b.size()/2 << endl; for(size_t i = 0; i < b.size(); i += 2) cout << b[i] << ' ' << b[i+1] << endl; } < https://en.wikipedia.org/wiki/Line_segment>:a line segment is a part of a line that is bounded by two distinct end points In this problem, the end points of a "segment" are not required to be distinct (first occurrence in test 4). program zad; var a,n,i:longint; b:array[1..1000] of real; begin n:=0; while not eof do begin read(a); n:=n+1; b[n]:=sqrt(a); end; for i:=n downto 1 do writeln(b[i]:0:4); end. As I think, u should use dynamic array. #include <iostream> #include <vector> using namespace std; int main(int argc, const char * argv[]) { char c; int total = 0; vector<char> ab1; vector<char> ab2; vector<char> ab3; for (char a = 'a'; a <= 'z'; a+= 3) { ab1.push_back(a); ab2.push_back(a + 1); ab3.push_back(a + 2); } ab1.push_back('.'); ab1.push_back('#'); ab2.push_back(','); ab2.push_back(' '); ab3.push_back('!');
while (cin.get(c)) {
if (find(ab1.begin(), ab1.end(), c) != ab1.end()) total += 1;
if (find(ab2.begin(), ab2.end(), c) != ab2.end()) total += 2;
if (find(ab3.begin(), ab3.end(), c) != ab3.end()) total += 3;
}
cout << total; } Find all the "1-positions" and find answers in logN. Total complexity is O(NlogN) #include <iostream> #include <set> using namespace std; int main() { int n; cin >> n; set<unsigned int> v; unsigned int a = 1, c = 0; while(a <= (1<<31)) { v.insert( a ); a += c++; } for(int i = 0; i < n; ++i) { cin >> a; if(*v.find(a) == v.size()) { cout << 0 << ' '; } else { cout << 1 << ' '; } } } Edited by author 07.02.2009 10:56 Solve it first in any language, only then ask for my solution. Edited by author 08.02.2009 03:20 Hints: 1. Do not use many 'new' operators. I used only one int array to store data. 2. Avoid PrintWriter.printf/String.format for writing answer, it may be written using only int arithmetic. 3. Read input using StreamTokenizer or the following Scanner: class Scanner { InputStream in; char c; Scanner(InputStream in) { this.in = in; nextChar(); } void asserT(boolean e) { if (!e) { throw new Error(); } } void nextChar() { try { c = (char)in.read(); } catch (IOException e) { throw new Error(e); } } long nextLong() { while (true) { if ('0' <= c && c <= '9') { break; } asserT(c != -1); nextChar(); } long value = c - '0'; nextChar(); while ('0' <= c && c <= '9') { value *= 10; value += c - '0'; nextChar(); } return value; } int nextInt() { long longValue = nextLong(); int intValue = (int)longValue; asserT(intValue == longValue); return intValue; } } My Java solution better from all Java solutions: 0.171 s time & 862KB memory. I use only System.in & System.out. good like. Use something like System.out.print( (a+b)/2 ); if ( (a+b) % 2==0){ } else { System.out.println(".5"); } instead of System.out.printf or String.format if you want to get AC I got AC with 858KB memory I have tried two methods to solve this problem: 1) using PriorityQueue 2) sorting using Arrays.sort but bumped into MLE on 7th test again and again :( so more hints are welcomed :) Did you write your own heap or u are using complete different approach? Can anyone tell me how memory 1 Mb can be exeeded if I use the PriorityQueue with size = N/2+1 and four int variables ? How many bytes are consumed by one element of PQ ? TIA. I cannot solve this problem. could you tell me more hint via my email please? here is my e-mail => holyporing@hotmail.com i really want to do this. Thank you. how to send solution with java? in language list, i haven't got java. Please, send your java solution on e-mail lo.de.wash.boleem@mail.ru Edited by author 26.01.2014 19:26 Edited by author 26.01.2014 19:27 В FPC все работает, здесь же ... Вот код program f1; var f,f2:text; i,j:integer; a:array[1..130000] of real; begin assign(f,'input.txt'); reset(f); i:=0; While not eof(f) do begin inc(i); read(f,a[i]); end; close(f); assign(f2,'output.txt'); rewrite(f2); For j:=i downto 1 do writeln(f2,sqrt(a[j]):0:4); close(f2); End. |
|