Общий форумUse the formula: N=[a+(a+p-1)]*p/2,and you will got the formula of A: A=[ 2*N/P +1-p]/2 Loop p to find A,that's what someone else in this forum suggested. But even in this way some people got TLE as well.I guess you start looping P from 10^9... In fact you can find that 2N/P +1-P >0,solve this inequality,you will find p is <[1+sqrt(1+8N)]/2. You can cut the time of the loop down to half or even less in this way. I can't understand you I'll explain it to you in Chinese tomorrow...How did you find my post? this problem has solution O(sqrt(2N)) 'cause 2N = P(2A+P-1) N, P, A is natural, so P is divider of 2N, so algo is simple scan N, multiply N by 2, then for each divider of this new N less then sqrt(N) try to find p = divider or n / divider, a = (a - p + 1) / 2, where a supposed to be natural, if out p bigger than our saved one - replace saved one. out our bigger result Loop P from 44720 downto 1 and check out these two conditions to be true: 1) N>=P(P+1)/2; 2) P divides (N-P(P+1)/2). Break as fast as you find the first one. Then A=1+(N-P(P+1)/2)/P. If you still can't get it, look at the following lines. 1+2+3+4+5=15 2+3+4+5+6=20 3+4+5+6+7=25 4+5+6+7+8=30 Et cetera. >var n,k,ans1,ans2:longint; > >begin > read(n,k); > if n<k then write('0') else begin > ans1:=(n div k)*(n + n mod k); > ans2:=(n-k)*4; > if ans1>ans2 then write(ans1) else write(ans2); end; >end. My program is very short. My method is like yours and I can't find the bug in it. What was wrong with yours? For 8 3 answer is 21, not 20 I’ve AC, but I still have a question! My first program used “unsigned long” type, and didn’t pass Test 12 Then I changed all types to “unsigned long long” , and program was AC. So, my question is why my first program failed on Test12? As you know n,m<=2^31-1;So, the biggest result is 0xFFFFFFFD! This result can be represented by unsigned long! So, I didn’t announce my program, because it is bad style. I’m sure that those programmers who solved this issue can understand my question…. Best Wishes, Veniamin. Each of n and m is <= 2^31-1. Yes, they are unsigned long. But max unsigned long + 1 is? Overflow, of course !! Your solution doesn't print n or m exactly but you have to calculate either 2*m or 2*n, aren't you? Thanks.I also meet this question. > here is my source (C++): --------------------------- #include <stdio.h> int i;long sum=0; int n; void main() { scanf("%d",&n); if(n<1) for(i=1;i>=n;i--) sum+=i; else for(i=1;i<=n;i++) sum+=i; printf("%ld",sum); } -------------------------- Try to delete void because i did so , and my programm was right good Luck ! can you say which alhoritm it is? topologial sort? input 6 Zaitseva 21:38.2 Hauswald 21:21.0 Boulygina 22:04.4 Henkel 22:06.1 Wilhelm 21:11.1 Jonsson 22:05.8 My program: 4 Hauswald Henkel Wilhelm Zaitseva explain me please what's wrong??? why Henkel isn't in Test? it's code program z1821; var n,i,j,m,x,max,ol:integer; s,s1:string; names:array[1..100]of string; times:array[1..100]of integer; reznames:array[1..100] of string; begin readln(n); j:=1; for i:=1 to n do begin readln (s); while s[j]<>' ' do begin inc(j); end; names[i]:=copy(s,1,j); // delete(s1,1,length(s1)); // s1:=copy(s,j+1,2); // val(s1,times[i],ol); // delete(s1,1,2); // s1:=copy(s,j+4,2); // times[i]:=times[i]*600; // val(s1,x,ol); // times[i]:=times[i]+x*10; // delete(s1,1,1); // s1:=copy(s,j+7,1); // val(s1,x,ol); // times[i]:=times[i]+x; // j:=1; //it's input, don't look on it =) end; for i:=2 to n do times[i]:=times[i]-300*(i-1); max:=times[1]+1; m:=0; j:=1; for i:=1 to n do begin if times[i]<max then begin max:=times[i]; reznames[j]:=names[i]; m:=m+1; j:=j+1; end; end; for i := 1 to m-1 do for j := 1 to m-i do if reznames[j] > reznames[j+1] then begin s1 := copy(reznames[j],1,length(reznames[j])); reznames[j] :=copy(reznames[j+1],1,length(reznames[j+1])); reznames[j+1] :=copy(s1,1,length(s1)); end; writeln(m); for i := 1 to m do writeln(reznames[i]); end. Edited by author 09.10.2012 23:47 #include<stdio.h> int main(void){ int n,k,time,n1,i; scanf("%d%d",&n,&k); i=1; n1=1; time=0; while(n1<k && n1<n){ n1=n1+i; i++; time++; } if(n - n1>0){ if((n-n1) % k == 0) time=time+(n-n1)/k; else time=time+(n-n1)/k+1; } printf("%d",time); return 0; } Hints: 1) your while loop condition is not quite right. 2) you are not growing n1 properly in the while loop 3) your "if (n - n1>0)" is not necessary, if n-n1 == 0 then the division result will be zero 4) integer ceiling of x/y can be done in a single step like this: (x + y - 1)/y 5) it's generally a good idea to finish each line of output with a newline character In general: do some examples on paper, and step through your code in the debugger, or put in a bunch of print statements to follow the progress of the solution, and you'll discover where the problems are. I don't know what problem in my program. That's why I ask you to check my tests: 100 100 1 1 50 50 9996 100 100 5 3 65 34 7783 5 6 1 1 5 6 9 2 2 1 2 2 1 1 5 5 1 1 3 3 24 I am waiting your comments. Thank you. Hello! The answer for this test: 2 2 1 2 2 1 is "2", but your answer is "1". Good luck! Thank you Master! I'm so careless)). Btw, in test #5 N = M = 100. I made inaccurate array definition and get this information. :) My program didn't pass #5 test, because not correctly process the case when N = 2, M > 2 or M = 2, N > 2. // 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. |
|