Common Board| Show all threads Hide all threads Show all messages Hide all messages | | C++ Answer | navi1893 | 1068. Sum | 2 Oct 2012 20:07 | 6 | #include <iostream> #include <cmath> using namespace std; int main() { int a, sum = 0; cin >> a; if( a > 0 ) { for ( int i = 1; i <= a; i++) sum += i; } else { for (int i = 1; i <= abs(a); i++) sum += i; sum = 1 - sum; } cout << sum; return 0; } hell whats problm with mine . #include <iostream> using namespace std; int main() { int A=0; double Ans=0; cin>>A; if(A<=10000) { if(A>=1) Ans=A*(A+1)/2; if(A<0) Ans=(A*-1)*(A-1)/2+1; cout<<Ans; } return 0; } What about less then 10000? WA 9? Floating types can return answer like 123e+10. So you have to do "int Ans" before got answ. In add: Just use formula s(a1,an)=(a1,an)/2*n like that
int(((1+n)/2)*(abs(1-n)+1))); Edited by author 24.08.2012 00:47 hello, whats problm with mine . #include <iostream> bool flag=true;//the number more than 0 int num=0; bool GetNum() { scanf("%d",&num); return num>=0; } int GetResoults() { //Sn=(a1+an)n/2 if (flag)//>0 { return (1+num)*num/2; } else//<0 { return (num+1)*((-num)+2)/2; } } int main() { flag=GetNum(); printf("%d\n",GetResoults()); return 0; } | | No subject | tt123753 | 1759. Long-Livers | 2 Oct 2012 18:30 | 3 | #include <iostream> #include <cstring> #include <algorithm> using namespace std; struct Date { int day;
int month;
int year; }; struct person { int num;
int live;
Date die; }; bool comp(person a, person b) { if (a.live != b.live) return a.live > b.live; if (a.die.year != b.die.year) return a.die.year < b.die.year; if (a.die.month != b.die.month) return a.die.month < b.die.month; if (a.die.day != b.die.day) return a.die.day < b.die.day; } bool leap(int year) { if (year%400==0) return 1; if (year%100==0) return 0; if (year%4==0) return 1; else return 0; } int monthDay(int year, int month) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31;
else if (month == 4 || month == 6|| month == 9 || month == 11) return 30;
else if (leap(year))return 29;
else return 28; } int Distance(Date a, Date b)//a??¨®¨²b o??e¨º?¨®????1¨¨??¨² { int dis = 0;
for (int i = a.year+1; i < b.year; ++i) { if (leap(i)) dis += 366;
else dis += 365; }
if (a.year != b.year) { for (int i = a.month+1; i <= 12; ++i) { dis += monthDay(a.year, i); }
dis += (monthDay(a.year, a.month) - a.day + 1); for (int i = 1; i < b.month; ++i) { dis += monthDay(b.year, i); }
dis += b.day; } else { if (a.month != b.month) { dis += (monthDay(a.year, a.month) - a.day + 1);cout << monthDay(a.year, a.month) << endl;
for (int i = a.month+1; i < b.month; ++i) { dis += monthDay(b.year, i); }
dis += b.day; } else { dis += (b.day - a.day + 1); } }
return dis; } int convert(char a, char b) { return (a - '0')*10 + b - '0'; } int convert(char a, char b, char c, char d) { return (a - '0')*1000 + (b - '0')*100 + (c - '0')*10 + d - '0'; } void print(Date a) { cout << a.year << " " << a.month << " " << a.day << endl; } int main() { int m;
cin >> m;
char useless[11];
Date a, b;
person p[101];
for (int i = 0; i < m; ++i) { scanf("%d.%d.%d", &a.day, &a.month, &a.year);
cin >> useless;
scanf("%d.%d.%d", &b.day, &b.month, &b.year);
p[i].num = i+1; p[i].live = Distance(a, b);
p[i].die = b; }
sort(p, p+m, comp);
cout << p[0].num << endl;
system("pause"); } I don't know why it always gets wrong answer; i know………… it's my fault... because i cout more sth. | | Problem 1325 "Dirt". Time limit now is 0.5 sec. | Sandro (USU) | 1325. Dirt | 2 Oct 2012 17:27 | 1 | Time limit decreased from 1 second to 0.5 second (it corresponds better to the original TL in 2004). The problem was rejudged. Many old solutions with TLE verdict got AC, while all new solutions working in time from 0.5 to 1 second got TLE. | | this is a good algorithm. | Aharon_Smbatyan(YSU) | 1009. K-based Numbers | 2 Oct 2012 15:31 | 18 | n , k 1. if ( n = 0 ) , then answer = ( 1 ). 2. if ( n = 1 ) , then answer = ( k-1 ). 3. else answer(n) = ( k-1 ) * ( answer(n-1,k) + answer(n-2,k) ). Edited by author 20.06.2010 04:04 Okay, I'll join to Captains Obvious. You can find an answer with O(1) memory and O(logN) time. Can you explain this algo? Really interesting to know better one. Thx. Okay, I'll join to Captains Obvious. You can find an answer with O(1) memory and O(logN) time. Sure, you're welcome. Okay, we're already know, that the answer can be found reccurently: F(N) = (K-1)*(F(K-1)+F(N-2)), where F(0) = 1, F(1) = K-1. We see, that the {F(N)} sequence is very similar to Fibonacci's one and can assume some facts. I see two different approaches to solve the problem. 1) First way is to build characteristic equation, solve it and get an exact formula for F(N). I didn't try this way, because I foresaw some problems with precision and float calculations. If you're interested, you can turn to this article as a manual: http://www.intuit.ru/department/algorithms/algocombi/8/2.html 2) I assumed, that method with fast matrix exponentiation will work as well as for Fibonacci's numbers. Just consider a matrix: L(N) = {{F(N+1), F(N)}, {F(N), F(N-1)}}. Let's try to find such R, that L(N)*R = L(N+1). If you write down this information, you'll easily see, that R = {{K-1, 1}, {K-1, 0}}. So all you need is to find L(1)*R.pow(N-2). Hope this information was helpful, I'm not sure it is the best way to describe an approach though. :-) Edited by author 16.08.2010 13:34Actually, I implemented only O(N)-solution before. Today I wrote a code for a fast exponentiation, but (it seems I don't know java well) my time increased in #1013, where BigInteger is needed. Strange. :-) Artem Khizha [DNU] Thx for explanations and idea! I got it! Really nice approach with matrixes! #include <iostream> using namespace std; unsigned long stepen(unsigned long K,unsigned long N) { unsigned long i, result=1; if (N<0) return 0; else { i=1; for(; i<=N; i++) result*=K; return result; } } int main() { unsigned long N, K, i, result; cin>>N; cin>>K; result=stepen(K, N)-(stepen(K,N-2)+stepen(K, N-1)-1); cout<<result<<endl; //system("Pause"); return 0;
} WA #2. Why? >>dima11221122 Firstly, I came up with something like that and got WA2 too. N = (k-1)^n + n(k-1)^(n-1) - that formula looks pretty alright; but it isn't. It assumes there can't be more than one zero in the number, while there obviously can. The actual answer is something like that - N = (k-1)^n + n * (П(i=1 to min(k,n)) of (k-i)^(n-1)) and i think it's anyways easier to solve that like original poster did. Btw, your code is sooooo bad; u might wanna do something about that. Edited by author 26.11.2011 06:42 n , k 1. if ( n = 0 ) , then answer = ( 1 ). 2. if ( n = 1 ) , then answer = ( k-1 ). 3. else answer(n) = ( k-1 ) * ( answer(n-1,k) + answer(n-2,k) ). Edited by author 20.06.2010 04:04 I am not sure, how the number of zero digit numbers (of course, without two zeros in it) become 1. Edited by author 29.03.2012 02:38Hi David 23 May 2012 11:28 Can anybody tell me where can i get some info about this topic(k-based numbers), I really don't get from where cames this recurrent formula? Greetings Re: Hi Amir Aupov [MIPT] 8 Aug 2012 19:02 How can we get valid n-digit number? Trivially, we can append each of (k-1) possible digits (k digits except for 0) to the front of each of (n-1)-digit valid numbers. But we can also form valid number this way: append 0 to the front of (n-2)-digit valid number, and then append (k-1) possible digits, as in the first case. This gives us F(n) = (k-1)*F(n-1)+(k-1)*F(n-2), where F is the function to yield the amount of valid n-digit numbers. I'd recommend you to ensure this yields only the valid numbers, think why can't it produce duplicates, and whether other means to construct valid numbers are possible. Then you follow Artem Khizha's [DNU] recipe and get an AC (= Edited by author 08.08.2012 19:05 Re: Hi Bogatyr 2 Oct 2012 15:31 > Amir Aupov [MIPT] I like your explanation. I approached the construction of N digit numbers from N-1 digit numbers in the typical way of constructing numbers: multiply N-1 digit number by base, and add in the new digits to the units position. This has the benefit of being clear and natural, so we can be sure that we're not missing any numbers. It has the disadvantage that you must track numbers that end in zero separately from those that don't. But this is still quite simple: Z(n): # of valid numbers ending in zero NZ(n): # of valid numbers not ending in zero Z(2) = k - 1 NZ(2) = (k - 1) * k - (k - 1) (total number of valid 2-digit numbers, minus those that end in 0) then, Z(n) = NZ(n - 1) NZ(n) = (Z(n-1) + NZ(n-1)) * (k - 1) Compute Z(n) and NZ(n) iteratively (DP), and then output F(n) at the end F(n) = Z(n) + NZ(n) Re: Hi Bogatyr 2 Oct 2012 14:32 HI David, The problem statement language is confusing. A "k-based number" is a number represented in base "k", that's all. | | I am very surprised to hear the fact!! | HaiyangZheng | 1001. Reverse Root | 1 Oct 2012 21:50 | 1 | | | переведите задание | efg | 1056. Centers of the Net | 1 Oct 2012 15:37 | 1 | | | Your tests are incorrect. | -XraY- | 1285. Thread in a Hyperspace | 1 Oct 2012 12:58 | 2 | There are a lot of tests (i know two: 4 and 9), in which r <= 0. Please, change it! Tests with r = 0 are fixed. Thank you. | | [java] Wrong answer test 3 - what is going on ? | Zawi | 1001. Reverse Root | 30 Sep 2012 19:33 | 2 | I do not understand what is wrong with my code it works ok on my PC: public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1")); char[] buffer = new char[262144]; br.read(buffer); Scanner in = new Scanner(new String(buffer)); Stack<String> lifo = new Stack<String>(); DecimalFormat format = new DecimalFormat("0.00000", new DecimalFormatSymbols(Locale.US)); while (in.hasNextLong()) { lifo.push(format.format(Math.sqrt(in.nextLong()))); } while(!lifo.empty()){ System.out.println(lifo.pop()); } } Could someone explain it to me ? Edited by author 30.09.2012 19:33 | | Hint | 吕蒙子明 | 1864. Get-Together at Den's | 30 Sep 2012 19:23 | 1 | Hint 吕蒙子明 30 Sep 2012 19:23 1: be careful of float point... 2: if there are K guys paid more than they drinked, the answer shouldn't be 100.0 / k, you should consider how much they paid... | | why WA on test 4, please give me some testcases. | Ycid | 1447. Portkey Network | 30 Sep 2012 10:04 | 1 | I use bs + MST(Prim), please help me!!! Here is my code: #include <iostream> #include <iomanip> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #define MAXN 1005 #define MAXE 500005 #define oo 1e100 #define EPS 1e-9 #define nabs(x) ((x) < 0? -(x) : (x)) using namespace std; struct edge { int to, l, c, back; edge() { } edge(int _to, int _l, int _c, int _back) { to = _to; l = _l; c = _c; back = _back; } } E[MAXE + MAXE]; int n, m, ce; int ptr[MAXN]; long double d[MAXN]; bool mark[MAXN]; void add_edge(int a, int b, int l, int c) { E[ce] = edge(b, l, c, ptr[a]); ptr[a] = ce++; E[ce] = edge(a, l, c, ptr[b]); ptr[b] = ce++; } long double Prim(long double x) { for (int i = 1; i < n + 1; i++) d[i] = oo; d[1] = 0; long double res = 0, _min, val; int v, w; memset(mark, 0, sizeof(mark)); for (int pass = 0; pass < n; pass++) { _min = oo; for (int i = 1; i < n + 1; i++) if (!mark[i] && d[i] < _min) { v = i; _min = d[i]; } mark[v] = true; res += _min; for (int i = ptr[v]; i != -1; i = E[i].back) { w = E[i].to; val = E[i].c - x * E[i].l; d[w] = min(d[w], val); } } return res; } int main() { int a, b, l, c; memset(ptr, -1, sizeof(ptr)); scanf("%d%d", &n, &m); while (m--) { scanf("%d%d%d%d", &a, &b, &l, &c); add_edge(a, b, l, c); } if(n == 1){ cout << "0" << endl; return 0; } long double lo = 0, hi = 1e15, mit, fmit, flo; for (; nabs(hi - lo) > EPS;) { mit = (lo + hi) / 2; fmit = Prim(mit); flo = Prim(lo); if (flo * fmit < 0) hi = mit; else lo = mit; } cout << setprecision(8) << lo << endl; } | | Compilation Error | Mara | | 30 Sep 2012 02:07 | 1 | No 1585 VAR pingvins:String; n,a,b,c,i:Integer; begin Readln(n); a:=0; b:=0; c:=0; FOR i:=1 TO n DO begin Readln(pingvins); Case pingvins OF 'Emperor Penguin' : a:=a+1; 'Little Penguin' : b:=b+1; 'Macaroni Penguin' : c:=c+1; end; end; IF (a>b) and (a>c) THEN Writeln('Emperor Penguin'); IF (b>a) and (b>c) THEN Writeln('Little Penguin'); IF (c>a) and (c>b) THEN Writeln('Macaroni Penguin'); end. Where is problem? | | AC | SODIQJOHN | 1880. Psych Up's Eigenvalues | 29 Sep 2012 22:15 | 3 | AC SODIQJOHN 27 Jun 2012 21:29 #include<iostream> #include<map> using namespace std; int main() { map<int,int>a; int n,k,i,m,l,p=0; cin>>n; for(i=0;i<n;i++) { cin>>k; a[k]++; }
cin>>m; for(i=0;i<m;i++) { cin>>k; a[k]++; }
cin>>l; for(i=0;i<l;i++) { cin>>k; a[k]++; if(a[k]==3){p++;} }
cout<<p; //system("pause"); return 0;} Re: AC Cảnh Toàn Nguyễn 1 Jul 2012 08:56 thanks for sharing your solution. Re: AC HaiyangZheng 29 Sep 2012 22:15 maybe, a is a very long array. I understood this method, but I have to worry the MLE...perhaps, "map" is magic... | | AC 0.015s, 120kb | Luka Bulatovic | 1457. Heating Main | 29 Sep 2012 16:16 | 4 | #include <iostream> #include <cstdio> using namespace std; int main() { int N; double sum; scanf("%d", &N); for(int i = 0; i < N; i++) { int p; scanf("%d", &p); sum += (double)p; } sum /= double(N); printf("%.6lf", sum); return 0; } Man you're awesome! I'm sure your solution is the best among 3000 others! Not true.You should initialize sum = 0 or it will be a trash number. Edited by author 09.09.2012 18:47 Edited by author 09.09.2012 18:47 | | WA #17. Why? | Alla | 1777. Anindilyakwa | 28 Sep 2012 17:01 | 2 | Here is my code: #include <iostream> using namespace std; int x[200]; int compare (const void *a, const void *b) { return (*(int*)a - *(int*)b); } int main() { int r[200], a = 3; long min; bool z = false; for (int i = 0; i<3; i++) cin>>x[i]; for ( ; ; ) { qsort (x, a, sizeof(int), compare); min = 1000000000; for (int i = 0; i<a-1; i++) { r[i] = abs(x[i] - x[i+1]); if (r[i]<min) min = r[i]; } x[a] = min; for (int j = 0; j<a; j++) { if (x[j] == min) { z = true; break; } } if (z == true) break; a++; } printf("%d", a - 1); return 0; } Where is my mistake? Use long long instead of int | | WA9 why there are some ki > i ? | yaoz | 1472. Martian Army | 28 Sep 2012 16:42 | 1 | " It is a tradition that a subordinate's computer has number that is greater than the number of his commander's computer. Each military person" why there are some ki > i ? in th 9th case, isn't it a tree with the root 1? | | Problem 1837 WA test #4 | TinyJanssen | | 28 Sep 2012 15:10 | 1 | What is special in test # 4? I tried all examples on the webboard and my program gives the correct result. Edited by author 28.09.2012 15:13 | | WA #2 Check 1 | Poochi | 1222. Chernobyl’ Eagles | 28 Sep 2012 11:18 | 2 | | | what is 4 test? | efg | 1869. New Year Cruise | 28 Sep 2012 04:10 | 1 | | | Probably incomplete task | Roman Mikhailov | 1100. Final Standings | 27 Sep 2012 20:41 | 2 | I'm received WA1 using standart bubble-sort (without saving original order). My answer for example task is correct (at least my solution coverred task conditions): ex. answer | my answer 3 5 |3 5 26 4 |22 4 22 4 |26 4 16 3 |20 3 20 3 |16 3 1 2 |11 2 11 2 |1 2 7 1 |7 1 In example task sorted array has saved original order. Maybe it cause wrong answer 1? At that case this important condition must be set in task. You are asked to write a program, which generates EXACTLY THE SAME final standings as old software Edited by author 27.09.2012 20:41 | | WA26 | liuzy | 1027. D++ Again | 27 Sep 2012 13:54 | 1 | WA26 liuzy 27 Sep 2012 13:54 I think my program is correct. Who can tell me why I got wa? #include<cstdio> #include<cstdlib> #include<cstring> char a[100005],b[100000]; char cs[]="=+-*/0123456789)("; bool pd(char ch){ for(int i=0;i<17;i++) if(ch==cs[i]) return 1; return 0; } int main(){ while(gets(b)){ strcat(a,b); } int len=strlen(a); int ha=0,k=0; for(int i=0;i<len;i++){ if(ha){ if(a[i]=='*' && a[i+1]==')') ha=0; }else{ if(a[i]=='*' && a[i-1]=='(') ha=1; else{ if(k>0 && (!pd(a[i]))){ printf("NO"); return 0; } if(a[i]=='(') k++; if(a[i]==')') k--; if(k<0) { printf("NO"); return 0;} } } } if(ha || k!=0) { printf("NO"); return 0; } else printf("YES"); system("pause"); return 0; } |
|
|