Common BoardDoes it mean in this problem h==2 ? Edited by author 11.11.2017 13:13 There needn't h. Main problem is that , find x, where y = g^x mod p. It's common problem, named discrete logarithm. And there are some 64 bit modulo multiplications. /* by thinking the combinations of the two locks,we get if the password number for lock1 is even or password number for lock2 is odd, the theif could open the lock. I guess that would be the easiest ideal,right? */ #include <iostream> using namespace std; int main() { int lock1, lock2; cin >>lock1 >>lock2; if (lock1 % 2 == 0 || lock2 % 2 != 0) cout << "yes" << endl; else cout << "no" << endl; return 0; } For the classic Josephus problem, there exists two classic algorithms. The algorithm A has time complexity O(lgn) and comes from <discrete mathematics>. /* Algorithm A */ int solve(int N/*The magic number 1999*/, int m/*length of the text*/) { long long z = 1; while (z < (N - 1LL/*int may overflow here*/) * m + 1) { z = (z * N + N - 2) / (N - 1); } return m * N - z; } The algorithm B is the classic O(n) algorithm. int solve(int N/*The magic number 1999*/, int m/*length of the text*/) { int z = 0; for (int k = 2; k <= m; ++k) { z = (z + N) % k; } return z; } In my submissions, algorithm A costs 0.015s, and algorithm B costs 0.001s. Maybe arithmetic operations for "long long" is a little too slow? Edited by author 19.02.2016 19:17 #include<bits/stdc++.h> using namespace std; typedef struct HeapNode { int *harr; int size; }node; int leftChild(int n) { return (2*n+1); } int rightChild(int n) { return (2*n+2); } int getParent(int n) { return (n-1)/2; } void maxHeapify(int n,node* nd) { int l=leftChild(n); int r=rightChild(n); int largest; if(l < nd->size && nd->harr[l] > nd->harr[n]) largest=l; else largest=n; if(r < nd->size && nd->harr[r] > nd->harr[largest]) largest=r; if(largest!=n) { int t=nd->harr[n]; nd->harr[n] = nd->harr[largest]; nd->harr[largest] =t; maxHeapify(largest,nd); } } void buildMaxHeap(node* node) { int i; int x=node->size-1; x=x/2; for(i=x;i>=0;i--) { maxHeapify(i,node); } } node* createHeap(int size) { node *n=(node*)malloc(sizeof(node)); n->size=size; n->harr=(int*)malloc(sizeof(int)*size); } void addElement(int n,node *nd) { int i=++nd->size; nd->harr=(int*)realloc(nd->harr,sizeof(int)*i); nd->harr[i-1]=n; buildMaxHeap(nd); } void deleteAt(int pos,node* nd) { int i; nd->harr[pos-1]=nd->harr[nd->size-1]; --nd->size; nd->harr=(int*)realloc(nd->harr,sizeof(int)*nd->size); i=pos-1; buildMaxHeap(nd); } int search(node *nd,int value) { int i=0; for(i=0;i<nd->size;i++) { if(nd->harr[i]==value) return i; } } int peek(node *nd) { return nd->harr[0]; } int main() { int m; cin>>m; node * Node=createHeap(m); int *arr=(int*)malloc(sizeof(int)*m); int *arr2=(int*)malloc(sizeof(int)*50001); int n=0; int i=0,j=0; while (n!=-1 && i<m) { cin>>n; arr[i++]=n; arr2[j++]=n; } Node->harr=arr; buildMaxHeap(Node); cout<<peek(Node)<<endl; int s=search(Node,arr2[0]); deleteAt(s+1,Node); int c=0,r=m-1; while(n!=-1) { c++; cin>>n; if(n==-1)break; arr2[r+c]=n; addElement(n,Node); cout<<peek(Node)<<endl; s=search(Node,arr2[c]); deleteAt(s+1,Node); } return 0; } this is my code... works fine in my compiler... can anyone say what is this access violation error for? It is not important. You can always add missing cards to the end of answer. Do you know this content, please? Why has array t = new Int16[32000, 32000]; always got a "Runtime Error"? Is "Memory exception in that case? I dont understand, that this size is incorrect! I use fenwick tree of two axis dimensions. Edited by author 15.11.2018 21:11 Edited by author 15.11.2018 21:11 Take care, it does not mean a closed interval. If you can store the closest base in the same column for each cell, then can you process the solution for each row? Look up "convex hull trick". If you're stuck think like this, having a directed segment AB, you just need a C among the remaining points s.t. <CBA is acute. Fixing A, is there a way to choose B such that any C among remaining will form acute <CBA ? That is test 2 Edited by author 14.11.2018 06:18 assc Edited by author 20.03.2019 19:54 My code gives true answers for all tests but when I send problem it gives WA in test 3. please write answer. test 3 is nothing, but 2 1000000. So how should the answer look like? Is it the biggest possible prime number? you are right, just prime number #include <bits/stdc++.h> using namespace std; #define int long long signed main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); int n,k; cin >> n >> k; int dontused=0,dontdie=0; int f; for(int i=0;i<n;i++) { cin >> f; if(k>f) { dontdie+=k-f; } if(k<f) { dontused+=f-k; } } cout << dontused << " " << dontdie; return 0; } What is the problem with my code? #include <iostream> using namespace std; #include <cmath> bool check_prime(double n) { int c; for (c=2;c<= sqrt(n);c++) { if((int) n%c==0) return 0; } if(c==(int) n) return 1; } int nth_prime(int n){ int m=1; while (n>0) { m++; if (m == 2) { n--; } if (m%2!=0) { if (check_prime(m)==1) { n--; } } } return m;
} int main() { int n,m,i,a[15000],s; scanf("%d",&n); for(i=0;i<n;i++){ scanf("%d",&a[i]); } for(i=0;i<n;i++){ s = nth_prime(a[i]); printf("%d\n",s); } return 0; } a=input() c=0 c+=a.count('a') c+=a.count('d') c+=a.count('g') c+=a.count('j') c+=a.count('m') c+=a.count('p') c+=a.count('s') c+=a.count('v') c+=a.count('y') c+=a.count('y') c+=a.count('.') c+=a.count(' ') c+=a.count('b')*2 c+=a.count('e')*2 c+=a.count('h')*2 c+=a.count('k')*2 c+=a.count('n')*2 c+=a.count('q')*2 c+=a.count('t')*2 c+=a.count('w')*2 c+=a.count('z')*2 c+=a.count(',')*2 c+=a.count('c')*3 c+=a.count('f')*3 c+=a.count('i')*3 c+=a.count('l')*3 c+=a.count('o')*3 c+=a.count('r')*3 c+=a.count('u')*3 c+=a.count('x')*3 c+=a.count('!')*3 print(c) I can't understand problem in my algorithm. Can it be solved with just Dijkstra? let A[i][j] be the number of i permutations that start with j and we jump 2. The recurrence is: a[i][1] = a[i - 1][1] + a[i - 1][2], a[i][2] = a[i - 1][2] + a[i - 3][2] I use the same methon...But I got Wa???? We can do it more easyly! Just think of this: 1 2 ... means N - 1; 1 3 2 4 means N - 3; 1 3 5 7...6 4 2 only 1; 1 3 4 2 (only N == 4); So we can DP; test You can using this: Initilization: a[1]:=1; a[2]:=1; a[3]:=2; And solve: a[i]:=a[i-1]+a[i-3]+1; So, your answer in a[n] I konw why I was WA...... Very thanks!!!! Well, actually I decovered this formula after writing brute force algorithm. I am wondering how it could be proven. Any ideas? Edited by author 10.04.2004 02:04 But Timgreen had almost proved it. write the permutations down and look it carefully as hard as you can and you'll find out sth useful for you associated with the informations provided above by all the upper friends... Can somebody give tests? for exmaple for 6,10,55... Thanks. for 6 , ans = 9 for 10, ans = 46 for 55, ans = 1388937263 I don't understand your formula . Could you explain ? Take i=5 for example The permutations are: 1 2 3 4 5 1 2 3 5 4 1 2 4 3 5 1 2 4 5 3 1 3 2 4 5 1 3 5 4 2 a[i-1] stands for the first four, beginning with '1 2'. If you ignore 1, you have the same prob for N=i-1. a[i-3] stands for the fifth, beginning with '1 3 2 4'. If you ignore 1, 3, 2, you have the same prob for N=i-3. And a special case (the last) -- all the odds in increasing order, followed by all the evens in decreasing order. The is what the +1 in the formula means. Good luck! thanks for explanation. I used another method to solve this. Suppose we have N numbers. We will care about five possible configurations: K(n) ... n n-1 ... T(n) ... n-1 n ... E(n) ... n-2 n P(n) ... n n-1 S(n) ... n-1 n here K(n) is the number of correct configurations of the first type and so on. It's clear that the answer for N will be K(N)+T(N)+E(N)+P(N)+S(N). So, what we need to do is understand how to get K(n+1),T(n+1)... from K(n),T(n),... And it is very easy to see that following is true: K(n+1) = T(n) T(n+1) = K(n)+P(n) E(n+1) = P(n) P(n+1) = S(n) S(n+1) = S(n)+E(n) I don't know if we can call this a proof. This is an observation I guess. Take i=5 for example The permutations are: 1 2 3 4 5 1 2 3 5 4 1 2 4 3 5 1 2 4 5 3 1 3 2 4 5 1 3 5 4 2 a[i-1] stands for the first four, beginning with '1 2'. If you ignore 1, you have the same prob for N=i-1. a[i-3] stands for the fifth, beginning with '1 3 2 4'. If you ignore 1, 3, 2, you have the same prob for N=i-3. And a special case (the last) -- all the odds in increasing order, followed by all the evens in decreasing order. The is what the +1 in the formula means. Good luck! My idea is : (a bit complicated) definition : dp[x][0][0] = [...] , x , x-1 , [...] dp[x][0][0] = [...] , x-1 , x-1 , [...] dp[x][1][0] = [...] , x , x-1 dp[x][1][0] = [...] , x-1 , x dp[x][1][0] = [...] , x-2 , x (dp[x][i][j] is number of sequences satisfying the corresponding rule) recurrence : dp[i][0][0] = dp[i-1][0][1]; dp[i][0][1] = dp[i-1][0][0] + dp[i-1][1][0]; dp[i][1][0] = dp[i-1][1][1]; dp[i][1][1] = dp[i-1][1][1] + dp[i-1][1][2]; dp[i][1][2] = dp[i-1][1][0]; answer (to be printed is) : dp[n][0][0] + dp[n][0][1] + dp[n][1][0] + dp[n][1][1] + dp[n][1][2] proof : left for you :) Edited by author 02.05.2013 19:18 I have observed the answers.I found that a[i]=a[i-1]+a[i-2]-a[i-5]; I still don't know why! Above has been shown: a[i] = a[i-1] + a[i-3] + 1 this holds for each i, then also: a[i-2] = a[i-3] + a[i-5] + 1 So a[i-2] - a[i-3] - a[i-5] = 1 and (substitute this result in the first equation) a[i] = a[i-1] + a[i-3] + a[i-2] - a[i-3] - a[i-5] which reduces to a[i] = a[i-1] + a[i-2] - a[i-5] Can you explain your formul?how should I use it!) cool, note that we can unite 3rd and 4th cases as one case when n >= 4. my formula is: a[i][1] = a[i-1][1] + a[i-2][2] a[i][2] = a[i-2][1] + 1 But i don't understand your formula? Could you explain?? Edited by author 14.07.2008 22:25 I've solved it with help of asympthotic. Starting from 20 - 25-th member (it can be found recursively) - a[i] ~ a[i-1]*(a[i-1]/a[i-2]) I use dfs to find the regular for some test n = 1 2 3 4 5 6 7 8 9 10 11 ans= 1 2 2 4 6 9 14 21 31 46 68 then I found f[n] = f[n-1] + f[n-2] - f[n-5] then I got AC. #include<stdio.h> int main() { int n, sum=0, i; scanf("%d", &n); if(n>0) { for(i=1; i<=n; i++) sum=sum+i; } else if(n<0) { for(i=n; i<=1; i++) sum=sum+i; } printf("%d", sum); return 0; } |
|