Common Board(a1 - a2) + (a2 - a3) + .... + (an - a1) == b1 + b2 + .... + bn == 0 -> we have some elements b < 0 and b >= 0 let dp[n][i][j] is cnt of three-primes numbers which 2 last digits is i and j, then dp[n+1][i][j]+=dp[n][k][i], if kij is three-prime number a = float(input("a:")) b = float(input("b:")) print(a+b) Edited by author 02.11.2019 14:57 Edited by author 02.11.2019 14:59 a = int(input()) b = int(input()) print(a+b) Runtime error . what ? You are trying to get 2 inputs but there is only one Check if your solution works correctly with dense graphs suppose everything is good for the first 'i' columns, but the column is 'i' bad, then let's try to find such 'j' that, by doing the operation, our new column 'i' will become good, what properties should the column 'j' have ? If you can find 'j' just do operation swap (i, j) from sys import stdin, stdout, float_info import re from math import sin, cos, acos, fabs CONST_PI = acos(-1.0) CONST_EPSILON = 2.2204460492503130808472633361816 # CONST_PI = 3.141592653589793 data = [] def solve(a, b, c, d): a = a + (c / 3600) + (b / 60) # Checking the latitude and longitude position to determine positive pr negative degree if d == 'SL' or d == 'WL': a = -a a = a * CONST_PI / 180 return a def distComp(a): if fabs(a) < CONST_EPSILON: return 0 elif a > 0: return 1 else: return -1 # Input data = stdin.read().split() # # Test Input # data = ['Message', '#513.', 'Received', 'at', '22:30:11.', 'Current', "ship's", 'coordinates', 'are', '41^46\'00"', 'NL', # 'and', '50^14\'00"', 'WL.', 'An', 'iceberg', 'was', 'noticed', 'at', '41^14\'11"', 'NL', 'and', '51^09\'00"', 'WL.', '==='] # data = ['Message', '#513.', 'Received', 'at', '22:30:11.', 'Current', "ship's", 'coordinates', 'are', '36^46\'00"', 'EL', # 'and', '50^14\'00"', 'WL.', 'An', 'iceberg', 'was', 'noticed', 'at', '41^14\'11"', 'NL', 'and', '76^09\'00"', 'WL.', '==='] # Message Received # messageReceived = [int(s) for s in re.findall(r'\b\d+\b', data[4])] # hh = messageReceived[0] # mm = messageReceived[1] # ss = messageReceived[2] # print('\nMessage Received at: ', hh, mm, ss) # Ship's Latitude shipLatitude = [float(s) for s in re.findall(r'\b\d+\b', data[9])] x1 = shipLatitude[0] x2 = shipLatitude[1] x3 = shipLatitude[2] x4 = data[10] # print('\nShip Latitude: ', x1, x2, x3, x4) # Ship's Longitude shipLongitude = [float(s) for s in re.findall(r'\b\d+\b', data[12])] y1 = shipLongitude[0] y2 = shipLongitude[1] y3 = shipLongitude[2] y4 = data[13][:2] # print('\nShip Longitude: ', y1, y2, y3, y4) # Ice Berg's Latitude iceBergLatitude = [float(s) for s in re.findall(r'\b\d+\b', data[19])] a1 = iceBergLatitude[0] a2 = iceBergLatitude[1] a3 = iceBergLatitude[2] a4 = data[20] # print('\nIce Berg Latitude: ', a1, a2, a3, a4) # Ice Berg's Longitude iceBergLongitude = [float(s) for s in re.findall(r'\b\d+\b', data[22])] b1 = iceBergLongitude[0] b2 = iceBergLongitude[1] b3 = iceBergLongitude[2] b4 = data[23][:2] # print('\nIce Berg Longitude: ', b1, b2, b3, b4) shipLatitudeResult = solve(x1, x2, x3, x4) shipLongitudeResult = solve(y1, y2, y3, y4) iceBergLatitudeResult = solve(a1, a2, a3, a4) iceBergLongitudeResult = solve(b1, b2, b3, b4) dist = 6875.0/2 dist = acos(sin(shipLatitudeResult) * sin(iceBergLatitudeResult) + cos(shipLatitudeResult) * cos(iceBergLatitudeResult) * cos(shipLongitudeResult - iceBergLongitudeResult)) * dist print('\nThe distance to the iceberg:', round(dist, 2), 'miles.') if dist < 100: print('DANGER!') Edited by author 30.03.2021 16:37 After sorting of array of x and y all you need to do is to compute this two double sums sum(i=1)^n sum(j=1)^(i-1) (x_i-x_j) and sum(i=1)^n sum(j=1)^(i-1) (y_i-y_j). After simplifications one can obtain that they equal sum(i=1)^n x_i*(2*i-1-n) and sum(i=1)^n y_i*(2*i-1-n). After computations you divide final sum by C_n^2=n*(n-1)/2 and get required answer. Can you make your Formulations readable? You can just count letters Edited by author 25.03.2021 01:13 Good afternoon! I am a programmer-beginner. I have written the code for this task on c++, but the time limit is exceeded on the second test! The code is this: ( https://ideone.com/Rh98zQ) #include <iostream> using namespace std; float ghj(int a) {int m; m=0; for(int l=1; l<a; l++) {if(a%l==0) {m=m+l;}} float c=(float(int(m))); float b=c/a; return b;} int main() { int i,j; cin >> i >> j; float p=ghj(i); int n=i; for(int o=i; o<=j; o++) {if(ghj(o)<p) {p=ghj(o); n=o;}} cout << n; return 0; } Tell me, please, how can I optimize the code? Is it dynamic programming on the profile? How to apply it? Yes, it is. Let dp[i][mask] be the maximal number of passengers we can sit on the first i rows such that none of them sit close to each other and the i'th row is occupied exactly as in the mask (0 <= mask <= 63, if the j'th bit in the mask is 1, then the seat (j + 1) is occupied, otherwise it is not). Now, to compute dp[i][mask] we need to iterate through all possible masks "prev" of the (i - 1)'st row. For each such valid mask prev ("valid" means that none of the passengers on the (i - 1)'st and i'th rows sit close to each other) we have to relax our answer by dp[i][mask] = max(dp[i][mask], dp[i - 1][prev] + (the number ones in the mask)). Additionally, we can maintain the previous mask pointer[i][mask] = prev for each dp[i][mask] which gives us the best answer. When done with computing dp, we have to run through all valid masks of the last row and check whether dp[n][mask] >= k. If there is no such mask, then the answer is impossible. Otherwise, remember this mask and easily restore the answer by using those pointers to "prev" masks. Edited by author 01.05.2020 17:30 Edited by author 01.05.2020 17:30 Simple recursion with set<string,int> memorisation also works May be tests are weak Wrong answer. Help pls. I had int32 overflow for the sum, switched to long and it worked who can tell me what the test6 is? Thanks If you are using sort in your algo and sorting pairs<char ch,int position>, you must compare this pairs by a "ch", but if "ch"`s are equal - you must compare "position". Comparsion function may be like that: struct s{char c;int p;}; bool cmp(s a, s b) { if(a.c!=b.c) { return a.c<b.c; } return a.p<b.p; } Use stable_sort instead of sort (if you're doing on C/C++) Getting WA on test 25, any test case? По условию, при входных n = 1 и единственном отрицательном значении ответ должен быть 0, но accepted получается и при неправильном решении такого теста... Либо я чего-то не понимаю... The test was added. Thank you. Input: 1a 1a 1a 1a 1a 1a 1a 1a 1a 1b 3b 5b 1c 1c Output: Tenpai what is wrong? #include <iostream> #include <cmath> #include <map> #include <vector> using namespace std; int main() { int n; string str; cin >> n >> str;
int k = str.size(); int i = 0; int sum = 1; if(n % k == 0) { while(i != n / k) { sum *= n - k * i; i++; } } else { while(i != n / k + 1) { sum *= n - k * i; i++; } sum *= n % k; }
cout << sum << endl;
return 0; } Edited by author 31.12.2015 12:38 It would be great if you stop searching telepathists and show error message. Is it compilation error? Add "#include <string>" line. Is it WA? Strange. Code looks working. Is it any another error? Show it. WA #10 Some people suppose n can be 20 so int is not enough for sum. Try to use long long sum. WA #10 Test: 5 !!! answer 10, your answer 20 5!!! here k=3, 5 isn't divisible by 3 so you have to multiply 5%3=2 with 5*(5-3) so the answer is 5*2*2=20 #include<iostream> //#include<string> using namespace std; int main() { int n; string s; cin>>n; cin>>s; int sum=n; int len = s.size(); //cout<<len<<endl; int i =1; while((n-i*len)>1) { sum*=(n-i*len); i++; } if(n%len!=0) cout<<(sum*(n%len))<<endl; else cout<<sum*len<<endl; return 0; } th: deleting leafes doesn't change centers |
|