Common BoardPage 1209 I am doing standard BFS, and then finding the max of all min over all shortest paths #include <bits/stdc++.h>(ignore this) using namespace std; void solve() { int n; int m; cin >> n >> m; vector<vector<int>> adj(n+1); for(int i=0;i<m;i++){ int a; int b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } vector<bool> vis(n+1,false); int s; int f; int r; cin >> s >> f >> r; vector<int> ds(n+1,0); vector<int> dr(n+1,0); //BFS from s queue<pair<int,int>> q; q.push({0,s}); ds[s] = 0; while(!q.empty()){ pair<int,int> v = q.front(); q.pop(); for(int j : adj[v.second]){ if(vis[j] == false){ ds[j] = v.first+1; vis[j] = true; q.push({ds[j],j}); } } } for(int i=1;i<=n;i++){ vis[i] = false; } // BFS from r q.push({0,r}); dr[r] = 0; while(!q.empty()){ pair<int,int> v = q.front(); q.pop(); for(int j : adj[v.second]){ if(vis[j] == false){ dr[j] = v.first+1; vis[j] = true; q.push({dr[j],j}); } } } for(int i=1;i<=n;i++){ vis[i] = false; } if(s == f){ cout << dr[s] << endl; return; } int ans = -1; // Final BFS q.push({dr[s],s}); bool found_f = false; while(!found_f){ int k = q.size(); while(k--){ pair<int,int> v = q.front(); q.pop(); for(int j : adj[v.second]){ q.push({min(dr[j],v.first),j}); if(j == f){ found_f = true; } } } } int k = q.size(); while(k--){ pair<int,int> v = q.front(); q.pop(); if(v.second == f) ans = max(ans,v.first); } cout << ans << endl; return; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; } Edited by author 09.07.2025 00:48 input: 1000000000000000000 output: 524638269999999991 optimized search + cross product) To read the input in C++, the following helped me: unsigned char x = cin.get(). Then I used some ifs in the form of if (x == 218) { ... } Also, the first test case seems to be different from the one suggested in the statement. b = input() s = '' d = [] for i in b: s += i if s == s[::-1]: d.append(s) if s != s[::-1] and len(s) % 2 != 0: s = s[1:] if s == s[::-1]: d.append(s) if s != s[::-1] and len(s) % 2 != 0: s = s[1:] m = max(d, key=len) if len(m) <= 1000: print(m) see what your program outputs for an integer answer ps it should output .00 посмотрите, что выводит ваша программа при целом ответе ps она должна выводить .00 #include <bits/stdc++.h> using namespace std; int cal(int x){ int ret = 0; while (x > 0){ ret += x % 10; x /= 10; } return ret; } bool lucky(int x, int mm){ int a = x / mm; int b = x % mm; if (cal(a) == cal(b)) return true; return false; } int main(){ int n; cin >> n; int nn = round(pow(10, n)) - 1; int mm = round(pow(10, n / 2)); int tot = 0; for (int i = 0; i <= nn; i++){ if (lucky(i, mm)) tot++; } cout << tot << endl; } if your code is simple and fast enough, brute force can work. what is wrong with my code: #include <iostream> #include <algorithm> #include <climits> #include <string> #include <cstring> #include <cmath> #include <vector> #include <stack> #include <map> #include <set> #include <iomanip> #include <unordered_map> #define ll long long #define fri(a, b) for (ll i = a; i < b; i++) #define frj(a, b) for (ll j = a; j < b; j++) #define frk(a, b) for (int k = a; k < b; k++) #define frh(a, b) for (int h = a; h < b; h++) #define frz(a, b) for (int z = a; z < b; z++) #define rfri(a, b) for (int i = a; i >= b; i--) #define rfrj(a, b) for (int j = a; j >= b; j--) #define yes cout << "YES" << "\n"; #define no cout << "NO" << "\n"; #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); const int mod=100000007; using namespace std; ll dp[50][1005]; ll func(ll n,ll s){ if(dp[n][s] != -1) return dp[n][s]; if(s == 0) return 1; if(n == 0){ if(s == 0) return 1; else return 0; } ll ans = 0; fri(0,10){ if((s - i) >= 0) ans = (ans + func(n-1,s-i)) ; } return dp[n][s] = ans; } int main() { fast ll T = 1; // cin >> T; frz(0,T){ ll n,s; cin >> n >> s; memset(dp,-1,sizeof(dp)); if(s%2) cout << 0 << "\n"; else { ll ans = func(n,s/2); cout << (ans*ans) << "\n"; } } } Page 1208 For example: 16 9001 9001 8999 8999 9001 8999 8999 9001 -9001 -9001 -9001 -8999 -8999 -9001 -8999 -8999 9001 -9001 8999 -8999 9001 -8999 8999 -9001 -9001 9001 -8999 8999 -9001 8999 -8999 9001 5 9000 9000 9000 -9000 -9000 -9000 -9000 9000 0 0 My AC program gives answer: 1 2 3 4 9 10 11 12 5 6 7 8 13 14 15 16 2 But right answer: 1 2 3 4 9 10 11 12 5 6 7 8 13 14 15 16 2 8 10 14 Hi, Admins. Please, add these case: N > 5*10^4 a[i] = 1, for i = 1.. N-1, and a[N] = 10^9 Q = N p[i] = i, x[i] = i For example: 77888 1 1 1 1 1 1 .... 1 1 77888 77888 1 1 2 2 3 3 4 4 5 5 ... 77888 77888 my solution takes 0.031 seconds. I tried with precalculating with map for setting up the index with 1 only rest will be auto 0 but miraculously WA at 4. so i figured out that the author only wants you to solve with his idea only. So good luck. These problems are trivial once you start thinking about things algebraically. The author made the constraints the way they are to try to force you in that direction as its an educational problem. DO your own solve then check this do not cheat yourself #include<bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define FastAF ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); template <typename T> // cin >> vector<T> istream &operator>>(istream &istream, vector<T> &v){for (auto &it : v) cin >> it;return istream;} template <typename T> // cout << vector<T> ostream &operator<<(ostream &ostream, const vector<T> &c){for (auto &it : c) cout << it << " ";return ostream;} const int mx=2e5; int ar[mx]; void d(){ ar[0]=2; int k=1; for(int i=3;i<mx;i++){ bool f=true; for(int j=2;j*j<=i;j++){ if(i%j==0){ f=false; break; } } if(f){ ar[k++]=i; } } } int main(){ FastAF d(); int n; cin>>n; while(n--){ int a;cin>>a; cout<<ar[--a]<<endl; } return 0; } Algo: sqrt with precomputation Edited by author 11.05.2025 11:58 Edited by author 11.05.2025 12:02 0 0 16 1 1 2 1 2 0 3 0 3 1 2 2 3 2 4 1 4 3 0 3 0 4 1 4 1 5 -1 5 -1 -1 1 -1 12.00000000000000000000 0 0 5 -1 -1 1 0 0 -1 2 -2 2 4 5.50000000000000000000 0 0 17 -1 -1 5 -1 1 0 1 1 2 2 2 1 3 1 3 3 4 4 4 3 5 3 5 5 0 5 0 6 6 6 6 7 -1 7 24.00000000000000000000 0 0 4 0 -1 1 0 0 1 -1 0 2.00000000000000000000 0 0 4 -1 2 -1 -1 1 0 3 -1 3.6666666666 0 0 3 -1 -1 1 -1 0 1 2.00000000000000000000 0 0 11 0 -1 3 0 2 2 1 1 2 3 0 4 0 3 -1 5 -2 3 0 2 -1 0 10.50000000000000000000 I would appreciate hints(or solution) a lot! Please e-mail me at addflash@dmc.chat Why is the sample 50.211, shouldn't the sample be 50.198 because the path (45, 0) -> (50, 1) -> (5, 1) -> (0, 0) is length 50.198? |
|