Common Board// modify precision #include <iostream> #include<math.h> #include<limits> using namespace std; int main () { //typedef std::numeric_limits<double>dbl; double A; double B; cin>>A; B=sqrt(A); cout.setf(ios::fixed); cout.precision(4); cout<<fixed<<B<<endl; return 0; } Edited by author 09.10.2012 02:55 Can you help me, plz? Edited by author 26.07.2006 15:15 Edited by author 27.07.2006 19:37 My program also WA#2. I have implemented 'inserted sort'. Sorts correctly. Don't know what's the problem... PS. What should output program if n == 0? I print '1'. Any right implemented sort (which passes time limit) gets AC. So I think you are mistaken. PS: for n=0 answer is empty output. Thank's. Problem was n=0. Why for n=0 output is empty(i agree that it get AC, but...)? Is it wrong? : 0 --> (one number in input)1-->1 what is the problem with output if n==0 ? If n=0 nothing should be output ! If n=0 nothing should be output ! <html> <title>Codeforces.com</title> <body bgcolor="#F0FF0F"> Test Yourself ! <a color="#FF0000" href=" http://codeforces.com">Codeforces.com</a> </body> </html> If n=0 nothing should be output ! I spent so much time to figure out my mistakes(You can see it from my attempts :) ). You can save your time by checking theese tests. Hope they are helpful. 4 unknown unknown unknown unknown Answer: 1 2 4 2 unknown unknown Answer: 1 2 12 A B unknown unknown unknown unknown unknown unknown unknown unknown unknown unknown Answer: 12 4 A B C D Answer: 4 8 A A B B unknown unknown unknown unknown Answer: 4 6 A A B B unknown unknown Answer: 3 6 unknown A A B unknown B Answer: 2 6 A unknown unknown A unknown unknown Answer: 1 6 A unknown A unknown unknown unknown Answer: 1 2 4 A A A B Answer: Igor is wrong. First and second tests are not correct. "It is guaranteed that Igor could recognize the language of at least one of the phrases." Some Others: 8 A unknown B unknown unknown unknown unknown unknown Answer: 4 8 10 unknown unknown unknown A unknown unknown unknown unknown B unknown Answer: 2 5 10 12 unknown A unknown unknown unknown B unknown unknown unknown C unknown unknown Answer: 3 4 6 12 12 unknown A unknown unknown unknown B unknown D unknown C unknown unknown Answer: 4 6 12 12 unknown A unknown unknown unknown B unknown D D C unknown unknown Answer: 4 My solution is the following: First read the input and count how many pieces are placed in the wrong boxes(i.e. there color is not the same as the color of the box) lets call that number cnt. You must move these pieces at least once. Then I build a graph for which the boxes are the nodes and there is an edge between two boxes a and b iff there is a piece of color b in the box of color a. After building the graph one should count the number of connected components in it(lets call it count). Now the answer to our problem is simply cnt + max(count-1,0) as one should do at least max(count-1,0) moving of his hand to another box and it is always possible to solve the problem in exactly that many moves of the hand without moving a piece. LSBG, thank you so much, it's the better solution I've ever seen))) In second test case, only possible way to block all possible routes is to control stations 2 and 3. But, both these stations are next to museum station, so we aren't able to control them! What's wrong? I've got AC, suggesting that there is exactly one station which is next to the station A - it's A. // PROGRAM 1263 IN C++ # include <iostream.h> # include <stdio.h> int main() { int N,M,a[10000]; double k,l=0; cin>>N>>M; if (N>=1 && N<=10000 && M>=1 && M<=10000) { for (int i=0;i<=M-1;i++) cin >>a[i]; for (int p=1;p<=N;p++) { for (int h=0;h<=M-1;h++) { if (a[h]==p) l++; } k=(l/M)*100; l=0; printf ("%.2f",k); cout <<'%'<<endl; } } return 0; } Edited by author 07.10.2012 21:43 Edited by author 07.10.2012 21:44 abracadabra arbadacarba In example 2 , why is this answer wrong : NO a r b a d a c a r b a All of them are "its nonempty prefix" ? All of them should be "non-empty" and "prefix" ... well in ur case "r" itself is not a prefix so ..this is WA hope this explains and helps.. Help needed! What is crash?(stack overflow) what to do?I use normal dfs and pass just two variables to it:((( write dfs without recursion or you can just add {$M 16777216} for Pascal and #pragma comment(linker, "/STACK:16777216") for C/C++ to your recursive solution. Thank you very much, I don't know how to express my thanks to you! Thanks !!! I also got Crash on Test #3. I use BFS with a global queue, and global vectors and arrays. Does anybody know why my program crashes. Here's my code: #include <cstdio> #include <vector> #include <queue> using namespace std; int a[5000][5000]; int dist[5000][5000]; vector <int> v[5000]; vector <int> used; queue <int> q; int parent[5000]; int n,m; int usd[5000]; void input() { scanf("%d",&n); int x,y,c; for (int i=0;i<n-1;i++) { scanf("%d%d%d",&x,&y,&c); a[x][y]=a[y][x]=c; v[x].push_back(y); v[y].push_back(x); } } void bld() { q.push(0); int c; while (!q.empty()) { c=q.front(); q.pop();
// printf("%d ",c);
usd[c]=1; for (int i=0;i<used.size();i++) { dist[c][used[i]]=dist[used[i]][c]=a[c][parent[c]]+dist[parent[c]][used[i]]; }
for (int i=0;i<v[c].size();i++) { if (usd[v[c][i]]==0) { // printf("%d - %d ",v[c][i],c); parent[v[c][i]]=c; q.push(v[c][i]); } } used.push_back(c); } } void print() { scanf("%d",&m); int x,y; for (int i=0;i<m;i++) { scanf("%d%d",&x,&y); printf("%d\n",dist[x][y]); } } int main() { input(); bld();
print(); return 0; } N ≤ 50000 but your arrays only 5000 I have this problem in Java. Could you help me to fix it? My solution crashes on this test case.I used BFS+LCA to solve it,used global queue and arrays of size 50005,which should be enough,as the problem says there will be 50000 nodes.I also used randomised source for BFS,but still it's not working.Can anyone please help? Here's my code #include<cstdio> #include<cstring> #include<queue> using namespace std; class edge{ public: int to,prev,w; }; edge edges[100100]; int last[100100],p[50005],d[50005],L[50005],P[50005][20]; int N,num; bool check[50005]; queue<int>Q; void addedge(int u,int v,int w) { edges[num].to=v; edges[num].w=w; edges[num].prev=last[u]; last[u]=num; num++; } void BFS(int sc) { int u,v,i; while(!Q.empty()) Q.pop(); Q.push(sc); check[sc]=true;p[sc]=-1;d[sc]=0;L[sc]=0; while(!Q.empty()) { u=Q.front(); Q.pop(); for(i=last[u]; i!=-1; i=edges[i].prev) {
v=edges[i].to; if(!check[v]) { check[v]=true; p[v]=u; d[v]=d[u]+edges[i].w; L[v]=L[u]+1; Q.push(v); } } } } int Find(int u,int v) { //make u at higher level if(L[u]>L[v]) { int temp=u; u=v; v=temp; } if(u==v) return u;
int log,i,j; for(log=1; log<N; log*=2); //bring u and v to same level for(i=log; i>=0 ;i--) if(L[v]-L[u]>=(1<<i)) v=P[v][i]; if(u==v) return u;
for(i=log; i>=0; i--){ if(P[u][i]!=-1 && P[u][i]!=P[v][i]) { u=P[u][i];v=P[v][i]; } } return p[u];
} int main() { int m,i,j,k,a,b,c,log,x; //freopen("timus.txt","r",stdin); while(scanf("%d",&N)!=EOF) { num=0; for(i=0; i<N; i++) last[i]=-1; for(i=1; i<N; i++) { scanf("%d%d%d",&a,&b,&c); x=a; addedge(a,b,c); addedge(b,a,c); } for(i=0; i<N; i++) check[i]=false; BFS(x); //for(i=0; i<N; i++) printf("%d %d %d %d\n",i,p[i],L[i],d[i]); memset(P,-1,sizeof(P)); for(log=1; log<N; log*=2); for(i=0; i<N; i++) P[i][0]=p[i]; for(j=1; j<=log; j++){
for(i=0; i<N; i++) { if(P[i][j-1]!=-1) P[i][j]=P[P[i][j-1]][j-1]; } }
scanf("%d",&m); while(m--) { scanf("%d%d",&a,&b); int LCA=Find(a,b); int dis=d[a]+d[b]-2*d[LCA]; printf("%d\n",dis); } }
return 0; } Edited by author 07.10.2012 01:43 Can anyone please help me?? Even this answer is ok right? because 'bra' isn't prefix WA #3 please suggest I do not have to take 1st string's legth in consideration... I am doing kmp failure function tabulation... for suffix match of string b with prefixes of a and thus ... only b's string length is taken care of .... plz suggest some test case where I might be wrong.. what m doing is this -------------------------- // n is the string length of b f.resize(n+2); f[0]=-1; int x; for(int i=1;i<=n;i++) { x=f[i-1]; while(x>=0&&a[x+1]!=b[i-1]) x=f[x]; if(a[x+1]==b[i-1]) f[i]=x+1; else f[i]=-1; } -------------------------------------------- Edited by author 07.10.2012 20:52 Pay attention to the fact that sizes of strings may distinguish. I do not have to take 1st string's legth in consideration... I am doing kmp failure function tabulation... for suffix match of string b with prefixes of a and thus ... only b's string length is taken care of .... plz suggest some test case where I might be wrong.. what m doing is this -------------------------- // n is the string length of b f.resize(n+2); f[0]=-1; int x; for(int i=1;i<=n;i++) { x=f[i-1]; while(x>=0&&a[x+1]!=b[i-1]) x=f[x]; if(a[x+1]==b[i-1]) f[i]=x+1; else f[i]=-1; } -------------------------------------------- I used stable_sort and had wrote this comparator: bool cmp(PII a, PII b) { return (a.first > b.first) } Man, you're genius. It was sarkazm? You need "Sarkazm" table, man. First time I use bubblesort, but TLE. What's going on?! I've now any errors and warnings. I'm using only <iostream.h>. > What's going on?! > I've now any errors and warnings. > I'm using only <iostream.h>. #include <iostream.h> int main(void) { unsigned int n; cin>>n; char *a,*val; unsigned int na; a=new char[2000]; val=new char[4]; if(n==0) { cout<<'0'; return 0; } a[0]='s'; a[1]='i'; a[2]='n'; a[3]='('; a[4]='1'; a[5]=')'; a[6]=0; na=6; unsigned int j; for(unsigned int i=0;i<n-1;i++) cout<<'('; for(unsigned int i=2;i<=n;i++){ cout<<a; cout<<'+'; cout<<(n-i+2); cout<<')'; //изменение а na=na-i+1; //игнорируем скобки справа if((i % 2)==0) a[na]=(char)'-'; else a[na]='+'; a[na+1]='s'; a[na+2]='i'; a[na+3]='n'; a[na+4]='('; na+=5; if(i<10){ val[0]=(char)('0'+i); val[1]=0; } else if(i<100) { val[0]=(char)('0'+i/10); val[1]=(char)('0'+i-i/10*10); val[2]=(char)0; } else { val[0]=(char)('0'+i/100); val[1]=(char)('0'+(i-i/100*100)/10); val[2]=(char)('0'+i-i/10*10); val[3]=(char)0; } j=0; while(val[j]!=0){ a[na]=val[j]; j++; na++; } for(j=0;j<i;j++) a[na+j]=')'; na+=j; a[na]=0; } cout<<a; cout<<"+1"; return 0; } > > > What's going on?! > > I've now any errors and warnings. > > I'm using only <iostream.h>. > #include <iostream.h> > > int main(void) > { > unsigned int n; > cin>>n; > char *a,*val; > unsigned int na; > > a=new char[2000]; > val=new char[4]; > > if(n==0) > { > cout<<'0'; > return 0; > } > > a[0]='s'; > a[1]='i'; > a[2]='n'; > a[3]='('; > a[4]='1'; > a[5]=')'; > a[6]=0; > na=6; > > unsigned int j; > > for(unsigned int i=0;i<n-1;i++) //~~~~~~~~~~~~~~~ > cout<<'('; > > for(unsigned int i=2;i<=n;i++){ //~~~~~~~~~~~~~~~redefinition > cout<<a; > cout<<'+'; > > cout<<(n-i+2); > cout<<')'; > > //§Ъ§Щ§Ю§Ц§Я§Ц§Я§Ъ§Ц §С > na=na-i+1; //§Ъ§Ф§Я§а§в§Ъ§в§е§Ц§Ю §г§Ь§а§Т§Ь§Ъ §г§б§в§С§У§С > if((i % 2)==0) > a[na]=(char)'-'; > else > a[na]='+'; > a[na+1]='s'; > a[na+2]='i'; > a[na+3]='n'; > a[na+4]='('; > na+=5; > if(i<10){ > val[0]=(char)('0'+i); > val[1]=0; > } > else if(i<100) > { > val[0]=(char)('0'+i/10); > val[1]=(char)('0'+i-i/10*10); > val[2]=(char)0; > } > else > { > val[0]=(char)('0'+i/100); > val[1]=(char)('0'+(i-i/100*100)/10); > val[2]=(char)('0'+i-i/10*10); > val[3]=(char)0; > } > j=0; > while(val[j]!=0){ > a[na]=val[j]; > j++; > na++; > } > for(j=0;j<i;j++) > a[na+j]=')'; > na+=j; > > a[na]=0; > } > > cout<<a; > cout<<"+1"; > > return 0; > } > > > THIS CODE WILL LEAD YOU TO AC (PAY ATTENTION TO CORRECTIONS): #include <iostream> //not # include <iostream.h> using namespace std; //to be able to use cin,cout and many other functions int main(void) { unsigned int n; cin>>n; char *a,*val; unsigned int na; a=new char[2000]; val=new char[4]; if(n==0) { cout<<'0'; return 0; } a[0]='s'; a[1]='i'; a[2]='n'; a[3]='('; a[4]='1'; a[5]=')'; a[6]=0; na=6; unsigned int j; for(unsigned int i=0;i<n-1;i++) cout<<'('; for(unsigned int i=2;i<=n;i++){ cout<<a; cout<<'+'; cout<<(n-i+2); cout<<')'; //изменение а na=na-i+1; //игнорируем скобки справа if((i % 2)==0) a[na]=(char)'-'; else a[na]='+'; a[na+1]='s'; a[na+2]='i'; a[na+3]='n'; a[na+4]='('; na+=5; if(i<10){ val[0]=(char)('0'+i); val[1]=0; } else if(i<100) { val[0]=(char)('0'+i/10); val[1]=(char)('0'+i-i/10*10); val[2]=(char)0; } else { val[0]=(char)('0'+i/100); val[1]=(char)('0'+(i-i/100*100)/10); val[2]=(char)('0'+i-i/10*10); val[3]=(char)0; } j=0; while(val[j]!=0){ a[na]=val[j]; j++; na++; } for(j=0;j<i;j++) a[na+j]=')'; na+=j; a[na]=0; } cout<<a; cout<<"+1"; return 0; } Edited by author 07.10.2012 01:31 Edited by author 07.10.2012 01:31 Program T1149; Var i,n:1..200; op:text; Procedure a(p:integer); Var i,j:integer; Begin For i:=1 to p do Begin write('sin'); if i=p then Begin write('(',i,')');break;end else If odd(i) then write('(',i,'-') else write('(',i,'+'); end; end; Procedure s(n:integer); Var i,j,k:integer; Begin k:=n; For i:=1 to n-1 do write('('); For i:=1 to n-1 do Begin a(i);write('+',k-i+1,')'); End; a(n); end; Begin Readln(n); s(n); For i:=1 to n-1 do write(')');write('+1'); Readln End. You get WA on test #1. Can't you fix it yourself? Why am i getting Crash with this solution in Java? Thank you. public static Vector<Integer> res=new Vector<Integer>(); public static class Tree{ public Tree l; public Tree r; int val; Tree(int v){ this.val=v; this.l=null; this.r=null; } public static void add(Tree t,int v){ if(v<t.val){ if(t.l==null){ t.l=new Tree(v); } else{ add(t.l,v); } } else if(v>t.val){ if(t.r==null){ t.r=new Tree(v); } else{ add(t.r,v); } } } public static void rightSearch(Tree t){ if(t.r!=null) rightSearch(t.r); if(t.l!=null) rightSearch(t.l); res.add(t.val); } } public static void main(String[] args) throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); int n=Integer.parseInt(in.readLine()); String[] input=in.readLine().split(" "); Tree t=new Tree(Integer.parseInt(input[n-1])); for(int i=n-2; i>=0; i--){ Tree.add(t,Integer.parseInt(input[i])); } Tree.rightSearch(t); for(int i=0; i<res.size(); i++){ out.print(res.get(i)+" "); } out.close(); } i found the mistake. Edited by author 07.10.2012 17:02 Edited by author 07.10.2012 17:34 Please,give me some hints or advices to solve this problem help fr5om mathemation. For me was interesting mathematical nature of the problem. I got Ac under next understanding. We must find volume of intersection [l.r] with dison union of [kx,ky] and [k0*x,infinity] Thank you for your answer. I'm sorry, but i did not understand your method. Can you describe your method more widely. Form disjoint segments:[kx,ky],k=1,2,3.. When [kx,ky] will intersect with [(k+1)x-1,(k+1)y] at k=k0:stop. From this moment we have [k0*x,infinity[ After standard programming task: find intersection of given segment[l,r] and disjoint system of closed segments. Edited by author 14.10.2009 17:30 Edited by author 15.10.2009 19:22 Congratulage! To implment that isue to be great! Very interesting problem, took me 2 days :). I started out wrong, but then saw the progression of [kx,ky] and the gaps between them. My solution subtracts the gaps from [l,r]. I had an initial misconception of the progression of the gaps, but corrected it and got AC. It's interesting that if y > x then the gaps will always shrink to size zero, eventually. Many people could find the formula, due to the inclusion-exclusion principle, but also many people could not find a proper method to calculate the formula, especially under module p. In fact, you don't need to calculate the huge combination numbers, nor do you need to perform some complex operations such as integer factorization. All you need is to rewrite the formula, and make it easier for calculation under module p. The formulation from inclusion-exclusion principle is: R = SUM (i = 0 to n) : (-1) ^ n * [(n + i)! / 2 ^ i] * C(n, i) where C(N, i) is the combination number, which is also the most difficult part in the formula. The key to calculation is rewrite the whole term, not only the combination number: [(n + i)! / 2 ^ i] * C(n, i) = [(n + i)! / 2 ^ i] * [n! / i! / (n - i)!] = [(n + i)! * n!] / [2 ^ i * i! * (n - i)!] = [(n + i)! / (n - i)!] / 2 ^ i * [n! / i!] Now, please note that, [(n + i)! / (n - i)!] could be recursively calculated: we need only to multiple (n + i) and (n + 1 - i) to the previous calculation result. Fortunately, there must be an even number in every pair of (n + i) and (n + 1 - i). So the increasing power of 2 could be also recursively canceled. Finally, the term [n! / i!] is easily calculated. Therefore, we solved the problem without a DIVISION calculation, which is hard to perform under module p. Hope this will help. nice xD one problem is that not everyone got exactly that formula, but instead another version of it. the fact that in your version of the formula you can obtain f(n) recursively only by multiplying two terms by f(n-1) makes it much easier. of course from the formula that i found i can get to yours, but thats more subtle. here is my code #include<stdio.h> int main() {
int n,k,i,m,l,p=0,a[100001]; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&k); a[k]++; } scanf("%d",&m); for(i=0;i<m;i++) { scanf("%d",&k); a[k]++; } scanf("%d",&l); for(i=0;i<l;i++) { scanf("%d",&k); a[k]++; if(a[k]==3){p++;} } printf("%d",p);
return 0;} Try a[1000000000+1], because k<=10^9. But in this case you have not enuf memory, so try another method. Edited by author 05.10.2012 15:20 Yermak Rating [1] 4 Oct 2012 14:51 What is it and how is it calculated? I think the rating is the sum of the difficulty of all problems you solve. So this rating will change if the ratings for each problem changes.... Correct me if I am wrong.. |
|