#include <iostream> #include <cmath> #define int long long using namespace std; int n,m; int t[1000000]; int cykl[1000000]; int minimal = 10000000; void debug(){ cout << endl; for (int i = 0; i < n; ++i) { cout << t[i]; } cout << endl; for (int i = 0; i < m; ++i) { cout << cykl[i]; } } int NWD(int a, int b){ if(a < b)NWD(b,a); if(b != 0)return NWD(b,a%b); return a; } int NWW(int a, int b){ return (a*b)/NWD(a,b); } main (){ ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) { int tmp; cin >> tmp; t[i] = tmp; if(tmp < minimal)minimal = tmp; } cin >> m; for (int i = 0; i < n; ++i) { char tmp; cin >> tmp; if(tmp == 'W')cykl[i] = 1; else cykl[i] = -1; } int a = 0; int b = 0; int moves = 0; bool check = false; while(true){ t[a] += cykl[b]; moves++; if(t[a] == 0){ cout << moves; break; } if(NWW(n,m) < moves && !check){ for (int i = 0; i < n; ++i) { if(t[i] < minimal)cout << -1; } check = true; break; } a++; b++; if(a>=n)a=0; if(b>=m)b=0; } cout << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <iostream> #include <cmath> #define int long long using namespace std; int n,m; int t[1000000]; int cykl[1000000]; int minimal = 10000000; void debug(){ cout << endl; for (int i = 0; i < n; ++i) { cout << t[i]; } cout << endl; for (int i = 0; i < m; ++i) { cout << cykl[i]; } } int NWD(int a, int b){ if(a < b)NWD(b,a); if(b != 0)return NWD(b,a%b); return a; } int NWW(int a, int b){ return (a*b)/NWD(a,b); } main (){ ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) { int tmp; cin >> tmp; t[i] = tmp; if(tmp < minimal)minimal = tmp; } cin >> m; for (int i = 0; i < n; ++i) { char tmp; cin >> tmp; if(tmp == 'W')cykl[i] = 1; else cykl[i] = -1; } int a = 0; int b = 0; int moves = 0; bool check = false; while(true){ t[a] += cykl[b]; moves++; if(t[a] == 0){ cout << moves; break; } if(NWW(n,m) < moves && !check){ for (int i = 0; i < n; ++i) { if(t[i] < minimal)cout << -1; } check = true; break; } a++; b++; if(a>=n)a=0; if(b>=m)b=0; } cout << endl; return 0; } |