├── A. Complex Operations ├── Author Sol.cc ├── Sadman - sort and search.cc ├── Zeron sol.cc └── Zico.cc ├── B. Zer VS EEE ├── Alter - reborn++.cc ├── Alternate Solution - Upobir .cc └── Zeron Main.cc ├── C. Mr. Xifu and His New School ├── Alter - reborn++.cc ├── Zeron Brute with assert.cc ├── himel2.0.cc └── solution by setter.cc ├── D. Rivalry Friends ├── Alter - reborn++ - precal.cc ├── Judge-Solution.cc └── Zico.cc ├── E. Fortis Fortuna Adiuvat ├── Sadman - Dijkstra.cc ├── fsshakkhor_0.2s_24MB.cc ├── ishtupeed_0.2s_26MB.cc └── ishtupeed_java_fastIO_0.6s_180MB.java ├── F. Piece of Cake ├── Alter - reborn++ - Appolo.cc ├── Alter - reborn++.cc ├── Alter Neerjhor.cc ├── Judge Solution.cc └── himel_cosine.cc ├── G. Is Bita Happy? ├── Alter - reborn++.cc ├── Judge Solution.cc └── Judge's Digit Dp Sol.cc ├── H. The Story of Stringland ├── Alter - reborn++.cc ├── Sadman-01.cc └── Shefin's Sol (Hash).cc ├── I. Solve This Giveaway Problem First ├── Judge Solution ( Hasinur_ ).cc ├── Shefin's Sol.cc └── alter-mainstring.java ├── J. Let's See! ├── Alter - reborn++.cc ├── Alter(Hasinur_).cc └── Judge Solution.cc ├── K. Happy Sub-Sequence ├── Alter-Hasnaine (Constraint Check).cc ├── Judge Solution.cc └── alter - mainstring.java ├── L. Information Retrieval ├── Alter - reborn++ - int id.cc ├── Alter - reborn++.cc ├── CLown1331.cc └── Original Solution.py ├── M. Ada Lovelace ├── Hasnaine alter in C.c ├── Judge Solution.cc ├── Python.py ├── Zeron alter in C.c ├── Zeron alter.cc └── c++.cc ├── NGPC2019 Analysis.pdf ├── NGPC2019 Problemset.pdf ├── readme.md └── toph-contest-ngpc-2019-problem-set.zip /A. Complex Operations/Author Sol.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | #define ll long long 8 | 9 | ll UB=(ll)(1e18); 10 | vectorv; 11 | int main(){ 12 | //freopen("input0.txt", "r", stdin); 13 | //freopen("output0.txt", "w", stdout); 14 | int n, q; 15 | cin>>n>>q; 16 | assert(n >= 1 && n <= 100000); 17 | assert(q >= 1 && q <= 100000); 18 | ll max_val = 0LL; 19 | for(int i = 0; i < n; ++i) { 20 | ll x; 21 | cin>>x; 22 | assert(x <= UB); 23 | max_val = max(max_val, x); 24 | v.push_back(x); 25 | } 26 | sort(v.begin(), v.end()); 27 | ll sm = 0LL; 28 | bool flg=false; 29 | while(q--){ 30 | ll op,x,y,z; 31 | cin>>op; 32 | if(op == 1){ 33 | cin>>x; 34 | if(sm <= max_val) { 35 | sm += x; 36 | } 37 | else flg=1; 38 | } 39 | else{ 40 | cin>>x>>y>>z; 41 | if(flg) { 42 | cout<<-1<= z) { 48 | cout< 2 | using namespace std; 3 | typedef long long ll; 4 | const int MX=100005; 5 | ll a[MX]; 6 | int main() 7 | { 8 | int n,q,k,t; 9 | ll l,r,x,y=0; 10 | scanf("%d %d",&n,&q); 11 | for(int i=0;i=n || a[i]>r) i=-1; 25 | ll ans=(i==-1)?-1:a[i]-y; 26 | printf("%lld\n",ans); 27 | } 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /A. Complex Operations/Zeron sol.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define ll long long 24 | 25 | #define sl(n) scanf("%lld", &n) 26 | #define ss(n) scanf("%s", n) 27 | #define sf(n) scanf("%lf", &n) 28 | 29 | #define PI 2*acos(0.0) 30 | #define INF (1LL<<62) 31 | 32 | #define pll pair 33 | 34 | #define pb push_back 35 | #define mp make_pair 36 | #define fr first 37 | #define sc second 38 | #define srt(v) sort(v.begin(), v.end()) 39 | using namespace std; 40 | 41 | ll a[100010]; 42 | 43 | int main() 44 | { 45 | // freopen("input.txt", "r", stdin); 46 | ll n, i, j, k, x, y, z, q, l, r; 47 | 48 | sl(n); 49 | assert(1 <= n && n <= 1e5); 50 | 51 | sl(q); 52 | assert(1 <= q && q <= 1e5); 53 | 54 | for (i = 0; i < n; i++) 55 | { 56 | sl(a[i]); 57 | assert(1 <= a[i] && a[i] <= 1e18); 58 | } 59 | 60 | sort(a, a+n); 61 | 62 | ll add = 0; 63 | 64 | while(q--) 65 | { 66 | sl(k); 67 | assert(k == 1 || k == 2); 68 | 69 | if (k == 1) 70 | { 71 | sl(x); 72 | assert(1 <= x && x <= 1e18); 73 | add += x; 74 | } 75 | else 76 | { 77 | sl(l); 78 | sl(r); 79 | sl(k); 80 | assert(1 <= l && l <= 1e18); 81 | assert(1 <= r && r <= 1e18); 82 | assert(1 <= k && k <= 1e5); 83 | 84 | ll lo = lower_bound(a, a+n, l+add) - a; 85 | ll hi = upper_bound(a, a+n, r+add) - a; 86 | 87 | if (hi - lo < k) 88 | printf("-1\n"); 89 | else printf("%lld\n", a[lo+k-1]-add); 90 | } 91 | } 92 | string temp; 93 | assert(!(cin >> temp)); 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /A. Complex Operations/Zico.cc: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | //using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | 7 | #define gc getchar unlocked 8 | #ifndef ONLINE JUDGE 9 | #define gc getchar 10 | #endif // ONLINE JUDGE 11 | 12 | #define pc putchar_unlocked 13 | #ifndef ONLINE JUDGE 14 | #define pc putchar 15 | #endif // ONLINE JUDGE 16 | 17 | #define fRead freopen("input.txt","r",stdin) 18 | #define fWrite freopen("output.txt","w",stdout) 19 | 20 | #define eps 1e-9 21 | #define inf 0x3f3f3f3f 22 | #define INF 2e18 23 | #define LL long long 24 | #define ULL unsigned long long 25 | #define PI acos(-1.0) 26 | #define pb push_back 27 | #define mk make_pair 28 | #define pii pair 29 | #define pLL pair 30 | #define ff first 31 | #define ss second 32 | #define all(a) a.begin(),a.end() 33 | #define rall(a) a.rbegin(),a.rend() 34 | #define SQR(a) ((a)*(a)) 35 | #define Unique(a) sort(all(a)),a.erase(unique(all(a)),a.end()) 36 | #define min3(a,b,c) min(a,min(b,c)) 37 | #define max3(a,b,c) max(a,max(b,c)) 38 | #define min4(a,b,c,d) min(min(a,b),min(c,d)) 39 | #define max4(a,b,c,d) max(max(a,b),max(c,d)) 40 | #define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e)) 41 | #define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e)) 42 | #define Iterator(a) __typeof__(a.begin()) 43 | #define rIterator(a) __typeof__(a.rbegin()) 44 | #define FOR(a,it) for(Iterator(a) it = a.begin();it != a.end(); it++) 45 | #define rFOR(a,it) for(rIterator(a) it = a.rbegin();it != a.rend(); it++) 46 | #define FastRead ios_base::sync_with_stdio(0);cin.tie(0) 47 | #define CasePrint pc('C'); pc('a'); pc('s'); pc('e'); pc(' '); write(qq++,false); pc(':'); pc(' ') 48 | #define vi vector 49 | #define vL vector 50 | #define For(I,A,B) for(int I = (A); I < (B); ++I) 51 | #define rFor(I,A,B) for(int I = (A); I >= (B); --I) 52 | #define Rep(I,N) For(I,0,N) 53 | #define REP(I,N) For(I, 1, N + 1) 54 | #define gti int, vi, greater 55 | #define gtL LL, vL, greater 56 | #define Found(a, b) a.find(b) != a.end() 57 | #define y0 y00 58 | 59 | const int MOD = 1e9 + 7; 60 | int fx[] = {-1,+0,+1,+0,+1,+1,-1,-1,+0}; 61 | int fy[] = {+0,-1,+0,+1,+1,-1,+1,-1,+0}; 62 | int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 63 | //template using orderset = tree , rb_tree_tag,tree_order_statistics_node_update>; // find_by_order, order_of_key 64 | template inline bool isLeap(T y) {return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false); } 65 | template inline T GCD (T a,T b ) {a = abs(a);b = abs(b); if(a < b) swap(a, b); while ( b ) { a = a % b; swap ( a, b ); } return a;} 66 | template inline T EGCD(T a,T b,T &x,T &y){if(a == 0) {x = 0;y = 1;return b;}T x1, y1;T d = EGCD(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;} 67 | template inline T LCM(T x,T y){T tp = GCD(x,y);if( (x / tp) * 1. * y > 9e18) return 9e18;return (x / tp) * y;} 68 | template inline T BigMod(T A,T B,T M = MOD){T ret = 1;while(B){if(B & 1) ret = (ret * A) % M;A = (A * A) % M;B = B >> 1;}return ret;} 69 | template inline T InvMod (T A,T M = MOD){return BigMod(A,M-2,M);} 70 | template void Compress(T * in, int n, int f = 0){vector vv;for(int i = f; i < n + f; i++) vv.pb(in[i]);Unique(vv);for(int i = f; i < n + f; i++) in[i] = lower_bound(all(vv), in[i]) - vv.begin();} 71 | //template void Compress(vector &in){vector vv;for(T x : in) vv.pb(x);Unique(vv);for(int i = 0; i < in.size(); i++) in[i] = lower_bound(all(vv), in[i]) - vv.begin();} 72 | /*---------------------------fast I/O------------------------------------*/ 73 | #define scani2(a,b) scani(a) , scani(b) 74 | #define scani3(a,b,c) scani(a), scani(b), scani(c) 75 | #define scani4(a,b,c,d) scani(a), scani(b), scani(c), scani(d) 76 | #define scani5(a,b,c,d,e) scani(a), scani(b), scani(c), scani(d) , scani(e) 77 | template T scani(T &n){n = 0;bool negative = false;char c = gc();while( c < '0' || c > '9'){if(c == '-') negative = true;c = gc();}while(c >= '0' && c <= '9'){n = n*10 + c-48;c = gc();}if(negative) n = ~(n-1);return n;} 78 | template void write(T n,int type = true){if(n<0) {pc('-');n = -n;}if(!n) {pc('0');if(type==32) pc(' '); else if(type) pc('\n'); return;}char buff[22];int len = 0;while(n) buff[len++] = n%10+48,n/=10;for(int i=len-1;i>=0;i--) pc(buff[i]);if(type==32) pc(' '); else if(type) pc('\n');} 79 | int scans(char *a){int i=0;char c = 0;while(c < 33) c = gc();while(c > 33){a[i++] = c;c = gc();}a[i] = 0;return i;} 80 | /*********************************************** End of template *********************************************/ 81 | 82 | const int N = 200005; /** need to update this **/ 83 | const int M = 200005; 84 | const ULL hs = 3797; 85 | 86 | int n, qr; 87 | LL in[N]; 88 | 89 | int main() 90 | { 91 | scani2(n, qr); 92 | assert(1 <= n && n <= 100000); 93 | Rep(i, n) { 94 | scani(in[i]); 95 | assert(0 <= in[i] && in[i] <= 1e18); 96 | } 97 | sort(in, in + n); 98 | LL ad = 0; 99 | while(qr--) { 100 | int type; LL l, r, k; scani(type); 101 | assert(1 <= type && type <= 2); 102 | if(type == 1) { 103 | scani(k); 104 | ad = min((LL)INF, ad + k); 105 | } else { 106 | scani3(l, r, k); 107 | assert(1 <= l && l <= 1e18); 108 | assert(1 <= r && r <= 1e18); 109 | l += ad; r += ad; 110 | 111 | if(ad >= INF) { 112 | write(-1); 113 | continue; 114 | } 115 | 116 | int small = upper_bound(in, in + n, l - 1) - in; 117 | 118 | k = k + small; 119 | LL res = -1; 120 | 121 | if(k && k <= n && (l <= in[k - 1] && in[k - 1] <= r)) res = in[k - 1] - ad; 122 | 123 | write(res); 124 | } 125 | assert(1 <= k && k <= 1e18); 126 | } 127 | return 0; 128 | } 129 | -------------------------------------------------------------------------------- /B. Zer VS EEE/Alter - reborn++.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | // Max Flow - Dinic 6 | 7 | #define INF 2000000000 8 | const int MAX_E=60003; 9 | const int MAX_V=5003; 10 | int ver[MAX_E], cap[MAX_E], nx[MAX_E], last[MAX_V], ds[MAX_V], st[MAX_V], now[MAX_V], edge_count, S, T; 11 | 12 | inline void reset() 13 | { 14 | memset(nx,-1,sizeof(nx)); 15 | memset(last,-1,sizeof(last)); 16 | edge_count=0; 17 | } 18 | inline void addedge(const int v,const int w,const int capacity,const int reverse_capacity) 19 | { 20 | ver[edge_count]=w; cap[edge_count]=capacity; nx[edge_count]=last[v]; last[v]=edge_count++; 21 | ver[edge_count]=v; cap[edge_count]=reverse_capacity; nx[edge_count]=last[w]; last[w]=edge_count++; 22 | } 23 | inline bool bfs() 24 | { 25 | memset(ds,-1,sizeof(ds)); 26 | int a,b; 27 | a=b=0; 28 | st[0]=T; 29 | ds[T]=0; 30 | while (a<=b) 31 | { 32 | int v=st[a++]; 33 | for (int w=last[v];w>=0;w=nx[w]) 34 | { 35 | if (cap[w^1]>0 && ds[ver[w]]==-1) 36 | { 37 | st[++b]=ver[w]; 38 | ds[ver[w]]=ds[v]+1; 39 | } 40 | } 41 | } 42 | return ds[S]>=0; 43 | } 44 | int dfs(int v,int cur) 45 | { 46 | if (v==T) return cur; 47 | for (int &w=now[v];w>=0;w=nx[w]) 48 | { 49 | if (cap[w]>0 && ds[ver[w]]==ds[v]-1) 50 | { 51 | int d=dfs(ver[w],min(cur,cap[w])); 52 | if (d) 53 | { 54 | cap[w]-=d; 55 | cap[w^1]+=d; 56 | return d; 57 | } 58 | } 59 | } 60 | return 0; 61 | } 62 | inline long long flow() 63 | { 64 | long long res=0; 65 | while (bfs()) 66 | { 67 | for (int i=0;i> n >> m; 95 | for(int i=0; i> e[i].fr >> e[i].to >> e[i].lb >> e[i].ub; 97 | } 98 | 99 | reset(); 100 | S = 0, T = n + 3; 101 | for(int i=0; i 2 | using namespace std; 3 | typedef long long int ll; 4 | typedef unsigned long long int ull; 5 | typedef long double ld; 6 | 7 | ll mod = 1e9 + 7; 8 | const ld error = 1e-7; 9 | const double PI = acos(-1); //const ld PI = acosl(-1) 10 | 11 | #define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 12 | #define eq(x, y) (fabs((x)-(y))= 0) ? y: y+m; 24 | } 25 | 26 | const int inf = 1e9; 27 | const ll infl = 1e18; 28 | const int nmax = 5e5+10; 29 | ///=========================================== template ======================================================= 30 | 31 | 32 | typedef ll F; 33 | F infflow = 1e14+5; 34 | const int vmax = 105; 35 | 36 | struct edge 37 | { 38 | int v, w; //edge is v -> w, flow is in that direction too 39 | F cap, flow; 40 | F lo; 41 | edge(int _v = 0, int _w = 0, F _c = 0, F _lo = 0){ 42 | v = _v, w = _w, cap = _c, flow = 0, lo = _lo; 43 | } 44 | 45 | //u represents from which vertex we request the function 46 | F flowfrom(int u) {return (u == v)? flow : -flow;} 47 | int other(int u) {return (u == v)? w: v;} 48 | F rescap(int u) {return (u == v)? cap - flow: flow;} 49 | void sendflow(int u, F f) {flow += (u == v)? f: -f;} 50 | }; 51 | 52 | vector E; 53 | vector adj[vmax]; 54 | 55 | inline void addEdge(int v, int w, F c, F lo) //add edge from v to w 56 | { 57 | E.push_back(edge(v, w, c, lo)); 58 | adj[v].push_back(E.size()-1); 59 | adj[w].push_back(E.size()-1); 60 | } 61 | 62 | 63 | int level[vmax]; //level array 64 | int ptr[vmax]; //ptr to edges in adjacency list 65 | inline bool bfs(int s, int t) 66 | { 67 | memset(level, -1, sizeof(level)); 68 | queue q; 69 | q.push(s); 70 | level[s] = 0; 71 | while(!q.empty()){ 72 | s = q.front(); 73 | q.pop(); 74 | if(s == t) break; 75 | ptr[s] = 0; 76 | for(auto i: adj[s]){ 77 | int v = E[i].other(s); 78 | if(level[v] > -1 || E[i].rescap(s) == 0) continue; 79 | 80 | level[v] = level[s]+1; 81 | q.push(v); 82 | } 83 | } 84 | return (level[t] > -1); 85 | } 86 | 87 | F augment(int u, F flow, int t) //dfs 88 | { 89 | if(u == t || flow == 0) return flow; 90 | for(int &i = ptr[u]; i>n>>m; 122 | for(int i = 0; i>v>>w>>lo>>hi; 126 | addEdge(v, w, hi-lo, lo); 127 | acc[v] -= lo; 128 | acc[w] += lo; 129 | } 130 | 131 | F tmp1 = 0, tmp2 = 0; 132 | for(int i = 1; i<=n; i++){ 133 | if(acc[i] > 0){ 134 | addEdge(0, i, acc[i], -1); 135 | tmp1 += acc[i]; 136 | } 137 | else if(acc[i] < 0){ 138 | addEdge(i, n+1, -acc[i], -1); 139 | tmp2 -= acc[i]; 140 | } 141 | } 142 | 143 | assert(tmp1 == tmp2); 144 | 145 | F tmp = maxflow(0, n+1); 146 | if(tmp != tmp1){ 147 | cout<<"NO\n"; 148 | return 0; 149 | } 150 | cout<<"YES\n"; 151 | for(int i = 0; i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #define ll long long 24 | #define lf double 25 | 26 | #define sl(n) scanf("%lld", &n) 27 | #define sf(n) scanf("%lf", &n) 28 | 29 | #define PI 2*acos(0.0) 30 | #define INFP ((ll)1<<62) 31 | #define INFN (0-(ll)1<<62) 32 | 33 | #define pb push_back 34 | #define mp make_pair 35 | #define ff first 36 | #define ss second 37 | #define srt(v) sort(v.begin(), v.end()) 38 | using namespace std; 39 | 40 | /* 41 | 42 | MAX FLOW Template code credit: Zobayer vai 43 | Link: http://zobayer.blogspot.com/2010/05/maximum-flow-dinitz-algorithm.html 44 | 45 | */ 46 | 47 | #define SET(p) memset(p, -1, sizeof(p)) 48 | #define CLR(p) memset(p, 0, sizeof(p)) 49 | 50 | const int INF = 1000000000; 51 | const int MAXN = 410, MAXE = 80010; //take node and edge twice in case node splitting 52 | 53 | int src, snk, nNode, nEdge; 54 | int Q[MAXN], last_edge[MAXN], pro[MAXN], dist[MAXN]; 55 | int flow[2 * MAXE], cap[2 * MAXE], prev_edge[2 * MAXE], to[2 * MAXE]; 56 | 57 | int edge_no[MAXN][MAXN]; 58 | 59 | inline void init(int _src, int _snk, int _n) 60 | { 61 | src = _src, snk = _snk, nNode = _n, nEdge = 0; 62 | SET(last_edge); 63 | } 64 | 65 | 66 | 67 | inline void add_edge(int u, int v, int c, int bi = 0) //bi = 0 for unidirected or directed 68 | { 69 | // cout << u << " -> " << v << " " << edge_no[u][v] << endl; 70 | assert(edge_no[u][v] == -1); 71 | edge_no[u][v] = nEdge; 72 | 73 | // cout << "edges " << u << " " << v << " " << c << endl; 74 | 75 | to[nEdge] = v, cap[nEdge] = c, flow[nEdge] = 0, prev_edge[nEdge] = last_edge[u], last_edge[u] = nEdge++; 76 | 77 | to[nEdge] = u, cap[nEdge] = bi == 1 ? c : 0, flow[nEdge] = 0, prev_edge[nEdge] = last_edge[v], last_edge[v] = nEdge++; 78 | 79 | } 80 | 81 | bool bfs() 82 | { 83 | int st, en, i, u, v; 84 | SET(dist); 85 | dist[src] = st = en = 0; 86 | Q[en++] = src; 87 | while (st < en) 88 | { 89 | u = Q[st++]; 90 | for (i = last_edge[u]; i >= 0; i = prev_edge[i]) 91 | { 92 | v = to[i]; 93 | if (flow[i] < cap[i] && dist[v] == -1) 94 | { 95 | dist[v] = dist[u] + 1; 96 | Q[en++] = v; 97 | } 98 | } 99 | } 100 | return dist[snk] != -1; 101 | } 102 | 103 | int dfs(int u, int fl) 104 | { 105 | if (u == snk) return fl; 106 | 107 | for (int &e = pro[u], v, df; e >= 0; e = prev_edge[e]) 108 | { 109 | v = to[e]; 110 | if (flow[e] < cap[e] && dist[v] == dist[u] + 1) 111 | { 112 | df = dfs(v, min(cap[e] - flow[e], fl)); 113 | if (df > 0) 114 | { 115 | flow[e] += df; 116 | flow[e ^ 1] -= df; 117 | return df; 118 | } 119 | } 120 | } 121 | return 0; 122 | } 123 | 124 | ll dinitz() 125 | { 126 | ll ret = 0; 127 | int df; 128 | while (bfs()) 129 | { 130 | for (int i = 0; i <= nNode; i++) pro[i] = last_edge[i]; 131 | while (true) 132 | { 133 | df = dfs(src, INF); 134 | if (df) ret += (ll)df; 135 | else break; 136 | } 137 | } 138 | return ret; 139 | } 140 | 141 | 142 | struct edd 143 | { 144 | ll u, v, lo, up; 145 | } ed[90010]; 146 | 147 | ll en[MAXN], ou[MAXN]; 148 | 149 | int main() 150 | { 151 | //freopen("IO/input01.txt", "r", stdin); 152 | // ios_base::sync_with_stdio(0); // no printf/scanf must be present 153 | ll cs, t, i, j, k, x, y, z, ans, m, p, q, n, num, sz = 0; 154 | 155 | sl(n); 156 | sl(m); 157 | 158 | assert(1 <= n && n <= 100); 159 | assert(1 <= m && m <= (n*n-n)); 160 | 161 | memset(edge_no, -1, sizeof(edge_no)); 162 | memset(en, 0, sizeof(en)); 163 | memset(ou, 0, sizeof(ou)); 164 | 165 | init(0, n + 1, n + 1); 166 | 167 | for (i = 1; i <= m; i++) 168 | { 169 | sl(ed[i].u); 170 | sl(ed[i].v); 171 | sl(ed[i].lo); 172 | sl(ed[i].up); 173 | 174 | assert(1 <= ed[i].u && ed[i].u <= n); 175 | assert(1 <= ed[i].v && ed[i].v <= n); 176 | assert(ed[i].u != ed[i].v); 177 | 178 | assert(0 <= ed[i].lo && ed[i].lo <= 1e6); 179 | assert(0 <= ed[i].up && ed[i].up <= 1e6); 180 | assert(ed[i].lo <= ed[i].up); 181 | 182 | en[ed[i].v] += ed[i].lo; 183 | ou[ed[i].u] += ed[i].lo; 184 | 185 | add_edge(ed[i].u, ed[i].v, ed[i].up - ed[i].lo); 186 | } 187 | 188 | 189 | for (i = 1; i <= n; i++) 190 | { 191 | if (en[i] >= ou[i]) 192 | add_edge(0, i, en[i] - ou[i]); 193 | else add_edge(i, n + 1, ou[i] - en[i]); 194 | } 195 | 196 | // cout << dinitz() << endl; 197 | 198 | dinitz(); 199 | 200 | bool flag = 1; 201 | 202 | for (i = 1; i <= n; i++) 203 | { 204 | if (edge_no[0][i] == -1) 205 | continue; 206 | 207 | if (flow[edge_no[0][i]] < cap[edge_no[0][i]]) 208 | { 209 | printf("NO\n"); 210 | flag = 0; 211 | break; 212 | } 213 | } 214 | 215 | if (flag == 0) 216 | return 0; 217 | 218 | printf("YES\n"); 219 | 220 | for (i = 1; i <= m; i++) 221 | { 222 | printf("%lld\n", flow[edge_no[ed[i].u][ed[i].v]] + ed[i].lo); 223 | } 224 | 225 | string temp; 226 | assert(!(cin >> temp)); 227 | return 0; 228 | } 229 | -------------------------------------------------------------------------------- /C. Mr. Xifu and His New School/Alter - reborn++.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef pair pii; 6 | 7 | int to_minutes(string ts) { 8 | assert(ts.size() == 5); 9 | assert(ts[2] == ':'); 10 | for(int i=0; i<5; ++i) { 11 | if(i != 2 and !isdigit(ts[i])) assert(false); 12 | } 13 | 14 | string hh = ts.substr(0, 2); 15 | string mm = ts.substr(3, 2); 16 | 17 | int hr = stoi(hh); 18 | int mn = stoi(mm); 19 | 20 | assert(hr >= 0 and hr < 24); 21 | assert(mn >= 0 and mn < 60); 22 | 23 | return hr * 60 + mn; 24 | } 25 | 26 | const int T = 24 * 60 + 7; 27 | int start[T], finish[T]; 28 | 29 | const int TC_MAX = 100; 30 | const int N_SUM_MAX = 1000000; 31 | 32 | int main() { 33 | ios::sync_with_stdio(false); 34 | cin.tie(0); cout.tie(0); 35 | 36 | int t, tc = 0; 37 | assert(cin >> t); 38 | assert(t > 0 and t <= TC_MAX); 39 | 40 | int n_sum = 0; 41 | 42 | while(t--) { 43 | int n; 44 | assert(cin >> n); 45 | assert(n > 0); 46 | n_sum += n; 47 | 48 | memset(start, 0, sizeof start); 49 | memset(finish, 0, sizeof finish); 50 | 51 | for(int i=0; i> st >> en); 54 | 55 | int fr = to_minutes(st); 56 | int to = to_minutes(en); 57 | 58 | if(fr >= to) { 59 | ++finish[24*60]; 60 | ++start[0]; 61 | } 62 | 63 | ++start[fr]; 64 | ++finish[to]; 65 | } 66 | 67 | int res = 0; 68 | for(int i=0, cnt=0; i> temp)); 79 | 80 | assert(n_sum <= N_SUM_MAX); 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /C. Mr. Xifu and His New School/Zeron Brute with assert.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #define ll long long 26 | 27 | #define sl(n) scanf("%lld", &n) 28 | #define ss(n) scanf("%s", n) 29 | #define sf(n) scanf("%lf", &n) 30 | 31 | #define PI 3.141592653589 32 | #define INF (1LL<<62) 33 | 34 | #define pll pair 35 | 36 | #define eps 1e-9 37 | #define pb push_back 38 | #define mp make_pair 39 | #define fr first 40 | #define sc second 41 | #define srt(v) sort(v.begin(), v.end()) 42 | using namespace std; 43 | 44 | ll csum[1440]; 45 | 46 | int main() 47 | { 48 | ll n, i, j, k, x, y, z, ts, cs, hs, ms, he, me, ss, se; 49 | 50 | sl(ts); 51 | assert(1 <= ts && ts <= 100); 52 | 53 | for (cs = 1; cs <= ts; cs++) 54 | { 55 | memset(csum, 0, sizeof(csum)); 56 | ll ans = 0; 57 | sl(n); 58 | assert(1 <= n && n <= 100000); 59 | 60 | for (i = 1; i <= n; i++) 61 | { 62 | scanf("%lld:%lld %lld:%lld", &hs, &ms, &he, &me); 63 | 64 | assert(0 <= hs && hs <= 23); 65 | assert(0 <= he && he <= 23); 66 | 67 | assert(0 <= ms && ms <= 59); 68 | assert(0 <= me && me <= 59); 69 | 70 | ss = hs * 60 + ms; 71 | se = he * 60 + me; 72 | 73 | j = ss; 74 | while(1) 75 | { 76 | csum[j]++; 77 | ans = max(ans, csum[j]); 78 | j++; 79 | if (j == 23*60 + 59 + 1) 80 | j = 0; 81 | if (j == se) 82 | break; 83 | } 84 | } 85 | printf("Case #%lld: Mr.Xifu needs %lld classroom(s)\n", cs, ans); 86 | } 87 | 88 | string temp; 89 | assert(!(cin >> temp)); 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /C. Mr. Xifu and His New School/himel2.0.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //#include 5 | //#include 6 | //using namespace __gnu_pbds; 7 | //template 8 | //using ordered_set=tree,rb_tree_tag,tree_order_statistics_node_update>; 9 | 10 | #define MAX 100005 11 | #define MOD 1000000007 12 | #define eps 1e-6 13 | int fx[] = {1,-1,0,0}; 14 | int fy[] = {0,0,1,-1}; 15 | 16 | #define FastRead ios_base::sync_with_stdio(0);cin.tie(0); 17 | #define fRead freopen("in.txt","r",stdin); 18 | #define fWrite freopen ("out.txt","w",stdout); 19 | 20 | #define LL long long 21 | #define ULL unsigned long long 22 | #define ff first 23 | #define ss second 24 | #define pb push_back 25 | #define PI acos(-1.0) 26 | #define mk make_pair 27 | #define pii pair 28 | #define pll pair 29 | #define all(a) a.begin(),a.end() 30 | 31 | #define min3(a,b,c) min(a,min(b,c)) 32 | #define max3(a,b,c) max(a,max(b,c)) 33 | #define min4(a,b,c,d) min(a,min(b,min(c,d))) 34 | #define max4(a,b,c,d) max(a,max(b,max(c,d))) 35 | 36 | #define FOR(i,a,b) for(int i=a;i<=b;i++) 37 | #define ROF(i,a,b) for(int i=a;i>=b;i--) 38 | #define REP(i,b) for(int i=0;i> t;FOR(tc,1,t) 44 | #define ABS(x) ((x)<0?-(x):(x)) 45 | #define SQ(x) ((x)*(x)) 46 | #define SP(x) fixed << setprecision(x) 47 | 48 | 49 | #define Make(x,p) (x | (1<> t; 69 | for(int tc=1;tc<=t;tc++) 70 | { 71 | int n; 72 | cin >> n; 73 | 74 | int arr[1500]={0}; 75 | 76 | for(int i=1;i<=n;i++) 77 | { 78 | string s,r; 79 | cin >> s >> r; 80 | int x=fnc(s); 81 | int y=fnc(r); 82 | 83 | if(xy){ 88 | arr[x]++; 89 | arr[1440]--; 90 | arr[0]++; 91 | arr[y]--; 92 | } 93 | else{ 94 | arr[0]++; 95 | arr[1440]--; 96 | } 97 | } 98 | 99 | for(int i=1;i<1500;i++) arr[i]+=arr[i-1]; 100 | int mx=0; 101 | for(int i=0;i<1440;i++) mx=max(mx,arr[i]); 102 | cout << "Case #" << tc << ": Mr.Xifu needs " << mx << " classroom(s)\n"; 103 | } 104 | } 105 | 106 | -------------------------------------------------------------------------------- /C. Mr. Xifu and His New School/solution by setter.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | using namespace std; 19 | 20 | 21 | 22 | int main() 23 | { 24 | //freopen("input_03.txt", "r", stdin); 25 | //freopen("output_03.txt", "wb", stdout); 26 | int tcase, i, ans, n, h1, m1, h2, m2, t1, t2, cs=1, cnt[2000], tot=24*60; 27 | scanf("%d", &tcase); 28 | 29 | while(tcase--) 30 | { 31 | memset(cnt, 0, sizeof cnt); 32 | scanf("%d", &n); 33 | while(n--) 34 | { 35 | scanf("%d:%d %d:%d", &h1, &m1, &h2, &m2); 36 | t1 = h1*60+m1; 37 | t2 = h2*60+m2; 38 | //printf("%d %d\n", t1, t2); 39 | if(t1 2 | using namespace std; 3 | 4 | const int M = 1e5 + 7; 5 | int phi[M]; 6 | 7 | // Shamelessly copied from ProgKriya. 8 | // Thanks smilitude! 9 | void calculatePhi() { 10 | for (int i = 1; i < M; i++) { 11 | phi[i] = i; 12 | } 13 | for (int p = 2; p < M; p++) { 14 | if (phi[p] == p) { // p is a prime 15 | for (int k = p; k < M; k += p) { 16 | phi[k] -= phi[k] / p; 17 | } 18 | } 19 | } 20 | } 21 | 22 | typedef long long ll; 23 | ll tr[4*M]; 24 | 25 | void build() { 26 | memset(tr, 0, sizeof tr); 27 | } 28 | 29 | void update(int at, int l, int r, const int p, const ll val) { 30 | if(l == r) { 31 | assert(p == l); 32 | tr[at] = val; 33 | return ; 34 | } 35 | 36 | int lc = at + at, rc = lc + 1, mid = (l + r) / 2; 37 | if(p <= mid) update(lc, l, mid, p, val); 38 | else update(rc, mid + 1, r, p, val); 39 | 40 | tr[at] = tr[lc] + tr[rc]; 41 | } 42 | 43 | ll query(int at, int l, int r, const int lo, const int hi) { 44 | if(l > hi or r < lo) return 0; 45 | if(l >= lo and r <= hi) return tr[at]; 46 | 47 | int lc = at + at, rc = lc + 1, mid = (l + r) / 2; 48 | ll q1 = query(lc, l, mid, lo, hi); 49 | ll q2 = query(rc, mid + 1, r, lo, hi); 50 | return q1 + q2; 51 | } 52 | 53 | const int SZ_MAX = 100000; 54 | const int Q_MAX = 100000; 55 | const int VAL_MAX = 100000; 56 | 57 | int main() { 58 | // ios::sync_with_stdio(false); 59 | // cin.tie(0); cout.tie(0); 60 | 61 | calculatePhi(); 62 | 63 | int n, q; 64 | cin >> n >> q; 65 | assert(n >= 1 and n <= SZ_MAX); 66 | assert(q >= 1 and q <= Q_MAX); 67 | 68 | build(); 69 | 70 | for(int i=1; i<=n; ++i) { 71 | int val; 72 | cin >> val; 73 | if(!(val >= 1 and val <= VAL_MAX)) { 74 | cout << "Array val: " << val << "\n"; 75 | } 76 | assert(val >= 1 and val <= VAL_MAX); 77 | 78 | update(1, 1, n, i, phi[val]); 79 | } 80 | 81 | while(q--) { 82 | int tp; 83 | cin >> tp; 84 | assert(tp == 1 or tp == 2); 85 | 86 | if(tp == 1) { 87 | int pos, val; 88 | cin >> pos >> val; 89 | 90 | assert(pos >= 1 and pos <= n); 91 | assert(val >= 1 and val <= VAL_MAX); 92 | 93 | update(1, 1, n, pos, phi[val]); 94 | } 95 | else { 96 | int fr, to; 97 | cin >> fr >> to; 98 | 99 | assert(fr >= 1 and fr <= n); 100 | assert(to >= 1 and to <= n); 101 | assert(fr <= to); 102 | 103 | ll res = query(1, 1, n, fr, to); 104 | cout << res << "\n"; 105 | } 106 | } 107 | 108 | string temp; 109 | if(cin >> temp) assert(false); 110 | 111 | return 0; 112 | } 113 | -------------------------------------------------------------------------------- /D. Rivalry Friends/Judge-Solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | typedef long long ll ; 5 | 6 | ll arr[100005],tree[4*100005], in [100006]; 7 | 8 | 9 | void init(int root, int b, int e ) 10 | { 11 | if(b==e) 12 | { 13 | tree[root]= arr[in[b]]; 14 | return; 15 | } 16 | ll left = root*2; 17 | ll right = (root*2) + 1; 18 | ll mid = (b+e)/2; 19 | init (left,b,mid); 20 | init(right,mid +1, e); 21 | tree[root]= tree[left]+tree[right]; 22 | } 23 | 24 | 25 | ll que(int root , int b, int e, int x , int y) 26 | { 27 | if(x>e||y=x&&e<=y)return tree[root]; 29 | ll right = root*2 +1; 30 | ll left = root*2; 31 | ll mid = (b+e)/2; 32 | ll p1= que(left,b,mid,x,y); 33 | ll p2 = que(right,mid+1,e,x,y); 34 | return p1+p2; 35 | } 36 | 37 | void update(int root , int b, int e, int x ,ll val) 38 | { 39 | if(x>e||x=x&&e<=x) 41 | { 42 | tree[root] = arr[val]; 43 | return; 44 | } 45 | ll right = root*2 +1 ; 46 | ll left = root*2 ; 47 | ll mid = (b+e)/2; 48 | update(left,b,mid,x,val); 49 | update(right,mid+1,e,x,val); 50 | tree[root]=tree[left]+tree[right]; 51 | } 52 | 53 | int phi(int n) 54 | { 55 | int result = n; 56 | for (int i = 2; i * i <= n; i++) 57 | { 58 | if(n % i == 0) 59 | { 60 | while(n % i == 0) 61 | n /= i; 62 | result -= result / i; 63 | } 64 | } 65 | if(n > 1) 66 | result -= result / n; 67 | return result; 68 | } 69 | 70 | 71 | int main() 72 | { 73 | 74 | // freopen("input_21.txt", "r", stdin); 75 | // freopen("output_21.txt", "w", stdout); 76 | 77 | for( int i = 1; i<=100000; i++) 78 | { 79 | arr[i] = phi(i); 80 | } 81 | 82 | int n , q ; 83 | scanf("%d%d",&n,&q); 84 | for( int i = 1 ; i <= n ; i ++) 85 | { 86 | scanf("%lld",&in[i]); 87 | } 88 | init(1,1,n); 89 | while( q-- ) 90 | { 91 | int type, i , j; 92 | ll val ; 93 | 94 | scanf("%d", &type); 95 | 96 | if( type == 1) 97 | { 98 | scanf("%d%lld",&i ,&val); 99 | update(1,1,n,i,val); 100 | } 101 | 102 | else 103 | { 104 | scanf("%d%d",&i,&j); 105 | printf("%lld\n", que(1,1,n,i,j)); 106 | } 107 | } 108 | 109 | 110 | } 111 | -------------------------------------------------------------------------------- /D. Rivalry Friends/Zico.cc: -------------------------------------------------------------------------------- 1 | #include 2 | //#include 3 | //using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | 7 | #define gc getchar unlocked 8 | #ifndef ONLINE JUDGE 9 | #define gc getchar 10 | #endif // ONLINE JUDGE 11 | 12 | #define pc putchar_unlocked 13 | #ifndef ONLINE JUDGE 14 | #define pc putchar 15 | #endif // ONLINE JUDGE 16 | 17 | #define fRead freopen("input.txt","r",stdin) 18 | #define fWrite freopen("output.txt","w",stdout) 19 | 20 | #define eps 1e-9 21 | #define inf 0x3f3f3f3f 22 | #define INF 2e18 23 | #define LL long long 24 | #define ULL unsigned long long 25 | #define PI acos(-1.0) 26 | #define pb push_back 27 | #define mk make_pair 28 | #define pii pair 29 | #define pLL pair 30 | #define ff first 31 | #define ss second 32 | #define all(a) a.begin(),a.end() 33 | #define rall(a) a.rbegin(),a.rend() 34 | #define SQR(a) ((a)*(a)) 35 | #define Unique(a) sort(all(a)),a.erase(unique(all(a)),a.end()) 36 | #define min3(a,b,c) min(a,min(b,c)) 37 | #define max3(a,b,c) max(a,max(b,c)) 38 | #define min4(a,b,c,d) min(min(a,b),min(c,d)) 39 | #define max4(a,b,c,d) max(max(a,b),max(c,d)) 40 | #define max5(a,b,c,d,e) max(max3(a,b,c),max(d,e)) 41 | #define min5(a,b,c,d,e) min(min3(a,b,c),min(d,e)) 42 | #define Iterator(a) __typeof__(a.begin()) 43 | #define rIterator(a) __typeof__(a.rbegin()) 44 | #define FOR(a,it) for(Iterator(a) it = a.begin();it != a.end(); it++) 45 | #define rFOR(a,it) for(rIterator(a) it = a.rbegin();it != a.rend(); it++) 46 | #define FastRead ios_base::sync_with_stdio(0);cin.tie(0) 47 | #define CasePrint pc('C'); pc('a'); pc('s'); pc('e'); pc(' '); write(qq++,false); pc(':'); pc(' ') 48 | #define vi vector 49 | #define vL vector 50 | #define For(I,A,B) for(int I = (A); I < (B); ++I) 51 | #define rFor(I,A,B) for(int I = (A); I >= (B); --I) 52 | #define Rep(I,N) For(I,0,N) 53 | #define REP(I,N) For(I, 1, N + 1) 54 | #define gti int, vi, greater 55 | #define gtL LL, vL, greater 56 | #define Found(a, b) a.find(b) != a.end() 57 | #define y0 y00 58 | 59 | const int MOD = 1e9 + 7; 60 | int fx[] = {-1,+0,+1,+0,+1,+1,-1,-1,+0}; 61 | int fy[] = {+0,-1,+0,+1,+1,-1,+1,-1,+0}; 62 | int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 63 | //template using orderset = tree , rb_tree_tag,tree_order_statistics_node_update>; // find_by_order, order_of_key 64 | template inline bool isLeap(T y) {return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false); } 65 | template inline T GCD (T a,T b ) {a = abs(a);b = abs(b); if(a < b) swap(a, b); while ( b ) { a = a % b; swap ( a, b ); } return a;} 66 | template inline T EGCD(T a,T b,T &x,T &y){if(a == 0) {x = 0;y = 1;return b;}T x1, y1;T d = EGCD(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;} 67 | template inline T LCM(T x,T y){T tp = GCD(x,y);if( (x / tp) * 1. * y > 9e18) return 9e18;return (x / tp) * y;} 68 | template inline T BigMod(T A,T B,T M = MOD){T ret = 1;while(B){if(B & 1) ret = (ret * A) % M;A = (A * A) % M;B = B >> 1;}return ret;} 69 | template inline T InvMod (T A,T M = MOD){return BigMod(A,M-2,M);} 70 | template void Compress(T * in, int n, int f = 0){vector vv;for(int i = f; i < n + f; i++) vv.pb(in[i]);Unique(vv);for(int i = f; i < n + f; i++) in[i] = lower_bound(all(vv), in[i]) - vv.begin();} 71 | //template void Compress(vector &in){vector vv;for(T x : in) vv.pb(x);Unique(vv);for(int i = 0; i < in.size(); i++) in[i] = lower_bound(all(vv), in[i]) - vv.begin();} 72 | /*---------------------------fast I/O------------------------------------*/ 73 | #define scani2(a,b) scani(a) , scani(b) 74 | #define scani3(a,b,c) scani(a), scani(b), scani(c) 75 | #define scani4(a,b,c,d) scani(a), scani(b), scani(c), scani(d) 76 | #define scani5(a,b,c,d,e) scani(a), scani(b), scani(c), scani(d) , scani(e) 77 | template T scani(T &n){n = 0;bool negative = false;char c = gc();while( c < '0' || c > '9'){if(c == '-') negative = true;c = gc();}while(c >= '0' && c <= '9'){n = n*10 + c-48;c = gc();}if(negative) n = ~(n-1);return n;} 78 | template void write(T n,int type = true){if(n<0) {pc('-');n = -n;}if(!n) {pc('0');if(type==32) pc(' '); else if(type) pc('\n'); return;}char buff[22];int len = 0;while(n) buff[len++] = n%10+48,n/=10;for(int i=len-1;i>=0;i--) pc(buff[i]);if(type==32) pc(' '); else if(type) pc('\n');} 79 | int scans(char *a){int i=0;char c = 0;while(c < 33) c = gc();while(c > 33){a[i++] = c;c = gc();}a[i] = 0;return i;} 80 | /*********************************************** End of template *********************************************/ 81 | 82 | const int N = 200005; /** need to update this **/ 83 | const int M = 200005; 84 | const ULL hs = 3797; 85 | 86 | int n, qr, phi[N], cur[N], in[N]; 87 | LL Bit[N]; 88 | 89 | void pre() { 90 | phi[1] = 1; 91 | for (int i = 2; i < N; i++) { 92 | if (phi[i] == 0) { 93 | phi[i] = i - 1; 94 | for (int j = (i << 1); j < N; j += i) { 95 | if(phi[j] == 0) phi[j] = j; 96 | phi[j] = phi[j] / i * (i-1); 97 | } 98 | } 99 | } 100 | } 101 | 102 | void add(int x, int v) { 103 | while(x < N) { 104 | Bit[x] += v; 105 | x += x & -x; 106 | } 107 | } 108 | 109 | LL query(int x) { 110 | LL ret = 0; 111 | while(x) { 112 | ret += Bit[x]; 113 | x -= x & -x; 114 | } 115 | return ret; 116 | } 117 | 118 | void printArray() { 119 | For(i, 1, n + 1) { 120 | cerr << cur[i] << ' '; 121 | } cerr << '\n'; 122 | } 123 | 124 | int main() 125 | { 126 | pre(); 127 | scani2(n, qr); 128 | assert(1 <= n && n <= 100000); 129 | assert(1 <= qr && qr <= 100000); 130 | 131 | For(i, 1, n + 1) { 132 | scani(in[i]); 133 | assert(1 <= in[i] && in[i] <= 100000); 134 | add(i, cur[i] = phi[ in[i] ]); 135 | } 136 | 137 | while(qr--) { 138 | int t, l, r, v; scani(t); 139 | assert(1 <= t && t <= 2); 140 | 141 | if(t == 1) { 142 | scani2(l, v); 143 | assert(1 <= l && l <= n); 144 | assert(1 <= v && v <= 100000); 145 | 146 | add(l, phi[v] - cur[l]); 147 | cur[l] = phi[v]; 148 | } else { 149 | scani2(l, r); 150 | assert(1 <= l && l <= n); 151 | assert(1 <= r && r <= n); 152 | 153 | write(query(r) - query(l - 1)); 154 | } 155 | } 156 | return 0; 157 | } 158 | -------------------------------------------------------------------------------- /E. Fortis Fortuna Adiuvat/Sadman - Dijkstra.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | typedef pair pii; 5 | const int MX=100005; 6 | ll dis[2][MX]; 7 | vector adj[2][MX]; 8 | void init(int n) 9 | { 10 | for(int i=0;i<=n;i++){ 11 | adj[0][i].clear(); 12 | adj[1][i].clear(); 13 | dis[0][i]=dis[1][i]=LLONG_MAX; 14 | } 15 | } 16 | void dijkstra(int n,int s,int k) 17 | { 18 | dis[k][s]=0; 19 | priority_queue ,greater> q; 20 | q.push({0,s}); 21 | while(!q.empty()){ 22 | int u=q.top().second; 23 | ll d=q.top().first; 24 | q.pop(); 25 | if(dis[k][u]!=d) continue; 26 | int l=adj[k][u].size(); 27 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | #define fRead(x) freopen(x,"r",stdin) 6 | #define fWrite(x) freopen (x,"w",stdout) 7 | 8 | #define LL long long 9 | #define ULL unsigned long long 10 | #define ff first 11 | #define ss second 12 | #define pb push_back 13 | #define INF 1e9 14 | #define PI acos(-1.0) 15 | #define mk make_pair 16 | #define pii pair 17 | #define pll pair 18 | 19 | 20 | #define min3(a,b,c) min(a,min(b,c)) 21 | #define max3(a,b,c) max(a,max(b,c)) 22 | #define min4(a,b,c,d) min(a,min(b,min(c,d))) 23 | #define max4(a,b,c,d) max(a,max(b,max(c,d))) 24 | #define SQR(a) ((a)*(a)) 25 | #define FOR(i,a,b) for(int i=a;i<=b;i++) 26 | #define ROF(i,a,b) for(int i=a;i>=b;i--) 27 | #define REP(i,b) for(int i=0;i >g1[N],g2[N]; 46 | LL dis1[N],dis2[N]; 47 | int n , m; 48 | 49 | void DJ(int s,LL dis[],vector>*g) 50 | { 51 | for(int i = 1;i <= n;i++)dis[i] = 1e18; 52 | priority_queueQ; 53 | Q.push({s,0}); 54 | dis[s] = 0; 55 | 56 | while(!Q.empty()){ 57 | point P = Q.top(); 58 | Q.pop(); 59 | int node = P.name; 60 | if(P.val != dis[node])continue; 61 | 62 | for(int i = 0;i < g[node].size();i++){ 63 | int go = g[node][i].first; 64 | int cost = g[node][i].second; 65 | if(dis[node] + cost < dis[go]){ 66 | dis[go] = dis[node] + cost; 67 | Q.push({go,dis[go]}); 68 | } 69 | } 70 | } 71 | } 72 | 73 | void solve() 74 | { 75 | int x, q; 76 | cin >> n >> m >> x >> q; 77 | assert(n >= 1 and n <= 100000 and m >= 1 and m <= 100000 and x >= 1 and x <= n and q >= 1 and q <= 100000); 78 | for(int i = 1;i <= n;i++)g1[i].clear(),g2[i].clear(); 79 | for(int i = 1;i <= m;i++){ 80 | int a,b,c; 81 | cin >> a >> b >> c; 82 | assert(a >= 1 and a <= 100000 and b >= 1 and b <= 100000 and c >= 1 and c <= 1000000000); 83 | g1[a].pb(mk(b,c)); 84 | g2[b].pb(mk(a,c)); 85 | } 86 | DJ(x,dis1,g1); 87 | DJ(x,dis2,g2); 88 | while(q--){ 89 | int s , t; 90 | cin >> s >> t; 91 | assert(s >= 1 and s <= n and t >= 1 and t <= n); 92 | LL ans = dis1[t] + dis2[s]; 93 | if(ans > 1e16){ 94 | cout << "Be seeing ya, John\n"; 95 | }else{ 96 | cout << ans << "\n"; 97 | } 98 | } 99 | 100 | } 101 | int main() 102 | { 103 | // fRead("test//test4.txt"); 104 | // fWrite("test//out4.txt"); 105 | 106 | FastRead 107 | int t,cases = 0; 108 | cin >> t; 109 | assert(t >= 1 and t <= 5); 110 | while(t--){ 111 | cout << "Case " << ++cases << ":\n"; 112 | solve(); 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /E. Fortis Fortuna Adiuvat/ishtupeed_0.2s_26MB.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #define pli pair 9 | vectorg1[100005]; 10 | vectorg2[100005]; 11 | long long dist1[100005]; 12 | long long dist2[100005]; 13 | long long maxVal = 1e18; 14 | 15 | void dijkstra1(int source) 16 | { 17 | priority_queue, greater >pq; 18 | pq.push(pli(0, source)); 19 | dist1[source] = 0; 20 | int u, sz, v; 21 | long long uw, vw, cost; 22 | while(!pq.empty()) 23 | { 24 | uw = pq.top().first; 25 | u = pq.top().second; 26 | // printf("Going from %d, distance %lld\n", u, uw); 27 | pq.pop(); 28 | if(uw != dist1[u]) 29 | continue; 30 | sz = g1[u].size(); 31 | for(int i = 0; i < sz; i++) 32 | { 33 | vw = g1[u][i].first; 34 | v = g1[u][i].second; 35 | // printf("To %d, cost %lld", v, vw); 36 | cost = dist1[u] + vw; 37 | if(dist1[v] > cost) 38 | { 39 | dist1[v] = cost; 40 | pq.push(pli(cost, v)); 41 | } 42 | } 43 | } 44 | } 45 | 46 | void dijkstra2(int source) 47 | { 48 | priority_queue, greater >pq; 49 | pq.push(pli(0, source)); 50 | dist2[source] = 0; 51 | int u, sz, v; 52 | long long uw, vw, cost; 53 | while(!pq.empty()) 54 | { 55 | uw = pq.top().first; 56 | u = pq.top().second; 57 | pq.pop(); 58 | if(uw != dist2[u]) 59 | continue; 60 | sz = g2[u].size(); 61 | // printf("Going from %d\n", u); 62 | for(int i = 0; i < sz; i++) 63 | { 64 | vw = g2[u][i].first; 65 | v = g2[u][i].second; 66 | cost = dist2[u] + vw; 67 | // printf("To %d, edge cost %lld, distance: %lld, previous: %lld\n", v, vw, cost, dist2[v]); 68 | if(dist2[v] > cost) 69 | { 70 | dist2[v] = cost; 71 | pq.push(pli(cost, v)); 72 | } 73 | } 74 | } 75 | } 76 | 77 | void initGraph(int n) 78 | { 79 | for(int i = 1; i <= n; i++) 80 | { 81 | g1[i].clear(); 82 | g2[i].clear(); 83 | dist1[i] = maxVal; 84 | dist2[i] = maxVal; 85 | } 86 | } 87 | 88 | int main() 89 | { 90 | int T, N, M, x, Q, u, v, s, t; 91 | int cases = 1; 92 | long long ans, w; 93 | scanf("%d", &T); 94 | assert(T<=5); 95 | while(T--) 96 | { 97 | scanf("%d %d %d %d", &N, &M, &x, &Q); 98 | assert(1 <= N && N <= 1e5 && 1 <= M && M <= 1e5 && 1 <= x && x <= N && 1 <= Q && Q <= 1e5); 99 | // printf("For %d nodes, %d edges, %d continental, %d queries\n", N, M, x, Q); 100 | initGraph(N); 101 | while(M--) 102 | { 103 | scanf("%d %d %lld", &u, &v, &w); 104 | assert(1 <= u && u <= N && 1 <= v && v <= N && 1 <= w && w <= 1e9); 105 | // printf("Going from %d to %d costs %d\n", u, v, w); 106 | g1[u].push_back(pli(w, v)); 107 | g2[v].push_back(pli(w, u)); 108 | } 109 | dijkstra1(x); 110 | dijkstra2(x); 111 | 112 | printf("Case %d:\n", cases++); 113 | while(Q--) 114 | { 115 | scanf("%d %d", &s, &t); 116 | assert(1 <= s && s <= N && 1 <= t && t <= N); 117 | ans = dist2[s] + dist1[t]; 118 | if(ans >= 1e16) 119 | puts("Be seeing ya, John"); 120 | else 121 | printf("%lld\n", ans); 122 | } 123 | } 124 | 125 | return 0; 126 | } 127 | -------------------------------------------------------------------------------- /E. Fortis Fortuna Adiuvat/ishtupeed_java_fastIO_0.6s_180MB.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedInputStream; 2 | import java.io.InputStream; 3 | import java.io.DataInputStream; 4 | import java.io.FileInputStream; 5 | import java.io.BufferedReader; 6 | import java.io.InputStreamReader; 7 | import java.io.OutputStream; 8 | import java.io.OutputStreamWriter; 9 | import java.io.BufferedWriter; 10 | import java.io.IOException; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | import java.util.Arrays; 14 | import java.util.PriorityQueue; 15 | 16 | 17 | public class Unnamed 18 | { 19 | int maxN = 100005; 20 | List >g1; 21 | List >g2; 22 | long[] dist1; 23 | long[] dist2; 24 | long maxVal = 1000000000000000000L; 25 | 26 | void initGraph(int n) 27 | { 28 | g1 = new ArrayList >(); 29 | g2 = new ArrayList >(); 30 | for (int i = 1; i < n + 5; i++) 31 | { 32 | List item = new ArrayList(); 33 | g1.add(item); 34 | } 35 | for (int i = 0; i < n + 5; i++) 36 | { 37 | List item = new ArrayList(); 38 | g2.add(item); 39 | } 40 | Arrays.fill(dist1, maxVal); 41 | Arrays.fill(dist2, maxVal); 42 | } 43 | 44 | void dijkstra1(int source) 45 | { 46 | // System.out.println("Dijkstra1"); 47 | // System.out.println("g1 size() = "+g1.size()); 48 | PriorityQueue pq = new PriorityQueue<>(); 49 | pq.add(new Node(source, Long.valueOf(0))); 50 | dist1[source] = 0; 51 | int u, sz, v; 52 | long uw, vw, cost; 53 | while(pq.size()!=0) 54 | { 55 | Node n = pq.poll(); 56 | u = n.f(); 57 | uw = n.s(); 58 | // System.out.println("Got node"+u); 59 | if(uw != dist1[u]) 60 | continue; 61 | sz = g1.get(u).size(); 62 | for(int i = 0; i < sz; i++) 63 | { 64 | Node m = g1.get(u).get(i); 65 | v = m.f(); 66 | vw = m.s(); 67 | cost = dist1[u] + vw; 68 | if(dist1[v] > cost) 69 | { 70 | dist1[v] = cost; 71 | pq.add(new Node(v, cost)); 72 | } 73 | } 74 | } 75 | } 76 | 77 | void dijkstra2(int source) 78 | { 79 | PriorityQueue pq = new PriorityQueue<>(); 80 | pq.add(new Node(source, Long.valueOf(0))); 81 | dist2[source] = 0; 82 | int u, sz, v; 83 | long uw, vw, cost; 84 | while(pq.size()!=0) 85 | { 86 | Node n = pq.poll(); 87 | u = n.f(); 88 | uw = n.s(); 89 | if(uw != dist2[u]) 90 | continue; 91 | sz = g2.get(u).size(); 92 | for(int i = 0; i < sz; i++) 93 | { 94 | Node m = g2.get(u).get(i); 95 | v = m.f(); 96 | vw = m.s(); 97 | cost = dist2[u] + vw; 98 | if(dist2[v] > cost) 99 | { 100 | dist2[v] = cost; 101 | pq.add(new Node(v, cost)); 102 | } 103 | } 104 | } 105 | } 106 | 107 | void solve() throws IOException 108 | { 109 | dist1 = new long[maxN]; 110 | dist2 = new long[maxN]; 111 | Reader cin = new Reader(); 112 | Print cout = new Print(); 113 | int T, N, M, x, Q, u, v, s, t; 114 | int cases = 1; 115 | long ans, w; 116 | T = cin.nextInt(); 117 | while(T != 0) 118 | { 119 | T--; 120 | N = cin.nextInt(); 121 | M = cin.nextInt(); 122 | x = cin.nextInt(); 123 | Q = cin.nextInt(); 124 | initGraph(N); 125 | while(M != 0) 126 | { 127 | M--; 128 | u = cin.nextInt(); 129 | v = cin.nextInt(); 130 | w = cin.nextLong(); 131 | 132 | g1.get(u).add(new Node(v, w)); 133 | g2.get(v).add(new Node(u, w)); 134 | } 135 | dijkstra1(x); 136 | dijkstra2(x); 137 | String c = "Case " + String.valueOf(cases) + ":"; 138 | cout.printLine(c); 139 | cases++; 140 | while(Q != 0) 141 | { 142 | Q--; 143 | s = cin.nextInt(); 144 | t = cin.nextInt(); 145 | ans = dist2[s] + dist1[t]; 146 | if(ans >= 10000000000000000L) 147 | { 148 | cout.printLine("Be seeing ya, John"); 149 | } 150 | else 151 | { 152 | String res = String.valueOf(ans); 153 | cout.printLine(res); 154 | } 155 | } 156 | } 157 | cout.close(); 158 | } 159 | 160 | 161 | public static void main(String[] args) 162 | { 163 | try 164 | { 165 | new Unnamed().solve(); 166 | } 167 | catch (IOException e) { 168 | e.printStackTrace(); 169 | } 170 | } 171 | 172 | // Node class for storing node name and weight 173 | public class Node implements Comparable 174 | { 175 | private int name; 176 | private long weight; 177 | 178 | public Node(int n, Long w) 179 | { 180 | this.name = n; 181 | this.weight = w; 182 | } 183 | 184 | public int f() 185 | { 186 | return name; 187 | } 188 | 189 | public long s() 190 | { 191 | return weight; 192 | } 193 | 194 | @Override 195 | public int compareTo(Node other) 196 | { 197 | long a = this.s(); 198 | long b = other.s(); 199 | if(a < b) 200 | return -1; 201 | if(a == b) 202 | return 0; 203 | return 1; 204 | } 205 | } 206 | 207 | // JAVA FAST IO 208 | static class Reader 209 | { 210 | final private int BUFFER_SIZE = 1 << 16; 211 | private DataInputStream din; 212 | private byte[] buffer; 213 | private int bufferPointer, bytesRead; 214 | 215 | public Reader() 216 | { 217 | din = new DataInputStream(System.in); 218 | buffer = new byte[BUFFER_SIZE]; 219 | bufferPointer = bytesRead = 0; 220 | } 221 | 222 | public Reader(String file_name) throws IOException 223 | { 224 | din = new DataInputStream(new FileInputStream(file_name)); 225 | buffer = new byte[BUFFER_SIZE]; 226 | bufferPointer = bytesRead = 0; 227 | } 228 | 229 | public String readLine() throws IOException 230 | { 231 | byte[] buf = new byte[64]; // line length 232 | int cnt = 0, c; 233 | while ((c = read()) != -1) 234 | { 235 | if (c == '\n') 236 | break; 237 | buf[cnt++] = (byte) c; 238 | } 239 | return new String(buf, 0, cnt); 240 | } 241 | 242 | public int nextInt() throws IOException 243 | { 244 | int ret = 0; 245 | byte c = read(); 246 | while (c <= ' ') 247 | c = read(); 248 | boolean neg = (c == '-'); 249 | if (neg) 250 | c = read(); 251 | do 252 | { 253 | ret = ret * 10 + c - '0'; 254 | } while ((c = read()) >= '0' && c <= '9'); 255 | 256 | if (neg) 257 | return -ret; 258 | return ret; 259 | } 260 | 261 | public long nextLong() throws IOException 262 | { 263 | long ret = 0; 264 | byte c = read(); 265 | while (c <= ' ') 266 | c = read(); 267 | boolean neg = (c == '-'); 268 | if (neg) 269 | c = read(); 270 | do { 271 | ret = ret * 10 + c - '0'; 272 | } 273 | while ((c = read()) >= '0' && c <= '9'); 274 | if (neg) 275 | return -ret; 276 | return ret; 277 | } 278 | 279 | public double nextDouble() throws IOException 280 | { 281 | double ret = 0, div = 1; 282 | byte c = read(); 283 | while (c <= ' ') 284 | c = read(); 285 | boolean neg = (c == '-'); 286 | if (neg) 287 | c = read(); 288 | 289 | do { 290 | ret = ret * 10 + c - '0'; 291 | } 292 | while ((c = read()) >= '0' && c <= '9'); 293 | 294 | if (c == '.') 295 | { 296 | while ((c = read()) >= '0' && c <= '9') 297 | { 298 | ret += (c - '0') / (div *= 10); 299 | } 300 | } 301 | 302 | if (neg) 303 | return -ret; 304 | return ret; 305 | } 306 | 307 | private void fillBuffer() throws IOException 308 | { 309 | bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 310 | if (bytesRead == -1) 311 | buffer[0] = -1; 312 | } 313 | 314 | private byte read() throws IOException 315 | { 316 | if (bufferPointer == bytesRead) 317 | fillBuffer(); 318 | return buffer[bufferPointer++]; 319 | } 320 | 321 | public void close() throws IOException 322 | { 323 | if (din == null) 324 | return; 325 | din.close(); 326 | } 327 | } 328 | 329 | class Print 330 | { 331 | private final BufferedWriter bw; 332 | public Print() 333 | { 334 | bw=new BufferedWriter(new OutputStreamWriter(System.out)); 335 | } 336 | public void print(String str)throws IOException 337 | { 338 | bw.append(str); 339 | } 340 | public void printLine(String str)throws IOException 341 | { 342 | print(str); 343 | bw.append("\n"); 344 | } 345 | public void close()throws IOException 346 | { 347 | bw.close(); 348 | } 349 | } 350 | } 351 | -------------------------------------------------------------------------------- /F. Piece of Cake/Alter - reborn++ - Appolo.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long double T; 6 | 7 | const T PI = acosl(-1); 8 | const T EPS = 1e-7; 9 | const T SIDE_MAX = 100000000; 10 | const T ANG_MAX = 180; 11 | const int TC_MAX = 100000; 12 | 13 | int main() { 14 | ios::sync_with_stdio(false); 15 | cin.tie(0); cout.tie(0); 16 | 17 | int t; 18 | assert(cin >> t); 19 | assert(t > 1 and t < TC_MAX); 20 | 21 | while(t--) { 22 | T ab, ad, bad; 23 | assert(cin >> ab >> ad >> bad); 24 | assert(ab > EPS and ab < SIDE_MAX); 25 | assert(ad > EPS and ad < SIDE_MAX); 26 | assert(bad > EPS and bad < ANG_MAX); 27 | 28 | bad *= PI / 180; 29 | // 2 (AB^2 + 2AD^2 - 2 AB AD cos > temp)); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /F. Piece of Cake/Alter - reborn++.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long double T; 6 | 7 | const T EPS = 1e-7; 8 | const T SIDE_MAX = 100000000; 9 | const T ANG_MAX = 180; 10 | const int TC_MAX = 100000; 11 | 12 | int main() { 13 | ios::sync_with_stdio(false); 14 | cin.tie(0); cout.tie(0); 15 | 16 | int t; 17 | assert(cin >> t); 18 | assert(t > 1 and t < TC_MAX); 19 | 20 | while(t--) { 21 | T ab, ad, bad; 22 | assert(cin >> ab >> ad >> bad); 23 | assert(ab > EPS and ab < SIDE_MAX); 24 | assert(ad > EPS and ad < SIDE_MAX); 25 | assert(bad > EPS and bad < ANG_MAX); 26 | 27 | bad *= (acosl(-1) / 180); 28 | 29 | T bd_sq = ab * ab + ad * ad - 2.0 * ab * ad * cosl(bad); 30 | T bd = sqrtl(bd_sq); 31 | T bc = bd + bd; 32 | 33 | // T sin_abd = ad * sinl(bad) / bd; 34 | // T abd = asin(sin_abd); 35 | 36 | assert(fabs(bd) > EPS); 37 | T cos_abd = (ab * ab + bd * bd - ad * ad) / (2.0L * ab * bd); 38 | T abd = acosl(cos_abd); 39 | 40 | T ac_sq = ab * ab + bc * bc - 2.0 * ab * bc * cosl(abd); 41 | T ac = sqrtl(ac_sq); 42 | 43 | T res = ab * ab + ac * ac; 44 | 45 | cout << fixed << setprecision(10) << res << "\n"; 46 | } 47 | 48 | string temp; 49 | assert(!(cin >> temp)); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /F. Piece of Cake/Alter Neerjhor.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #define pi acos(-1.0) 3 | 4 | using namespace std; 5 | 6 | typedef long long ll; 7 | 8 | int main () 9 | { 10 | int t; 11 | double ab, ad, bad; 12 | scanf("%d", &t); 13 | while (t--) 14 | { 15 | scanf("%lf%lf%lf", &ab, &ad, &bad); 16 | double bd_2 = ab*ab + ad*ad - 2.0*ab*ad*cos(bad*pi/180.0); 17 | double ans = bd_2 + ad*ad; 18 | ans = 2.0 * ans; 19 | printf("%.10f\n", ans); 20 | } 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /F. Piece of Cake/Judge Solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | long double AB,AD,A; 10 | int T; 11 | 12 | cin>>T; 13 | 14 | for (int t=1;t<=T;t++) 15 | { 16 | cin>>AB>>AD>>A; 17 | long double ang1=((A)*(2*acos(0)))/180; 18 | long double BD = sqrt((AB*AB) + (AD*AD) - (2*AB*AD*cos(ang1))); 19 | long double res1=2*((AD*AD)+(BD*BD)); 20 | 21 | cout< 2 | using namespace std; 3 | 4 | //#include 5 | //#include 6 | //using namespace __gnu_pbds; 7 | //template 8 | //using ordered_set=tree,rb_tree_tag,tree_order_statistics_node_update>; 9 | 10 | #define MAX 100005 11 | #define MOD 1000000007 12 | #define eps 1e-6 13 | int fx[] = {1,-1,0,0}; 14 | int fy[] = {0,0,1,-1}; 15 | 16 | #define FastRead ios_base::sync_with_stdio(0);cin.tie(0); 17 | #define fRead freopen("in.txt","r",stdin); 18 | #define fWrite freopen ("out.txt","w",stdout); 19 | 20 | #define LL long long 21 | #define ULL unsigned long long 22 | #define ff first 23 | #define ss second 24 | #define pb push_back 25 | #define PI acos(-1.0) 26 | #define mk make_pair 27 | #define pii pair 28 | #define pll pair 29 | #define all(a) a.begin(),a.end() 30 | 31 | #define min3(a,b,c) min(a,min(b,c)) 32 | #define max3(a,b,c) max(a,max(b,c)) 33 | #define min4(a,b,c,d) min(a,min(b,min(c,d))) 34 | #define max4(a,b,c,d) max(a,max(b,max(c,d))) 35 | 36 | #define FOR(i,a,b) for(int i=a;i<=b;i++) 37 | #define ROF(i,a,b) for(int i=a;i>=b;i--) 38 | #define REP(i,b) for(int i=0;i> t;FOR(tc,1,t) 44 | #define ABS(x) ((x)<0?-(x):(x)) 45 | #define SQ(x) ((x)*(x)) 46 | #define SP(x) fixed << setprecision(x) 47 | 48 | 49 | #define Make(x,p) (x | (1<> AB >> AD >> a; 60 | a=(a*PI)/180.0; 61 | 62 | double BD=sqrt(AB*AB+AD*AD-2.0*AB*AD*cos(a)); 63 | double BC=2.0*BD; 64 | 65 | // double b=asin((AD*sin(a))/BD); 66 | double b=acos((SQ(AB)+SQ(BD)-SQ(AD))/(2.0*AB*BD)); 67 | double AC=sqrt(AB*AB+BC*BC-2.0*AB*BC*cos(b)); 68 | cout << SP(7) << AB*AB+AC*AC << '\n'; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /G. Is Bita Happy?/Alter - reborn++.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | #define ODD 1 8 | #define EVEN 0 9 | 10 | // int check(int a, int pos, int fr, int to) { 11 | // ll sum = 0; 12 | // for(int i=fr; i<=to; ++i) { 13 | // if((a & i) & (1 << pos)) sum += i; 14 | // } 15 | // return sum % 2; 16 | // } 17 | 18 | // returns the parity of the number of 19 | // values < val where bit 'pos' is set 20 | int solve(int val, int pos) { 21 | int ret = EVEN; 22 | 23 | for(int i=30; i>=0; --i) { 24 | bool bit = (val & (1 << i)); 25 | 26 | if(pos == i) { 27 | if(!bit) break; 28 | continue; 29 | } 30 | 31 | if(!bit) continue; 32 | 33 | int c = i - (pos < i); 34 | if(pos == 0) { 35 | if(c != 0) ret ^= EVEN; 36 | else ret ^= ODD; 37 | } 38 | else { 39 | if(c != 1) ret ^= EVEN; 40 | else ret ^= ODD; 41 | } 42 | } 43 | 44 | return ret; 45 | } 46 | 47 | const int TC_MAX = 100000; 48 | const int ALR_MAX = 1000000000; 49 | const int BITPOS_MAX = 30; 50 | 51 | int main() { 52 | ios::sync_with_stdio(false); 53 | cin.tie(0); cout.tie(0); 54 | 55 | int t; 56 | assert(cin >> t); 57 | assert(t >= 1 and t <= TC_MAX); 58 | 59 | while(t--) { 60 | int a, k, l, r; 61 | assert(cin >> a >> k >> l >> r); 62 | assert(a >= 1 and a <= ALR_MAX); 63 | assert(k >= 0 and k <= BITPOS_MAX); 64 | assert(l >= 1 and l <= ALR_MAX); 65 | assert(r >= 1 and r <= ALR_MAX); 66 | 67 | int res = (((a & (1 << k)) > 0) & (solve(r+1, k) ^ solve(l, k))); 68 | // assert(res == check(a, k, l, r)); 69 | cout << (res == ODD ? "Odd" : "Even") << "\n"; 70 | } 71 | 72 | string temp; 73 | assert(!(cin >> temp)); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /G. Is Bita Happy?/Judge Solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | 6 | int main() 7 | 8 | { 9 | ll t; 10 | cin >> t; 11 | assert(1 <= t && t <= (ll)1e5); 12 | 13 | while(t--) { 14 | ll a, k, l, r; 15 | scanf("%lld %lld %lld %lld", &a, &k, &l, &r); 16 | assert(1 <= a && a <= (ll)1e9); 17 | assert(1 <= l && l <= (ll)1e9); 18 | assert(1 <= r && r <= (ll)1e9); 19 | assert(l <= r); 20 | assert(0 <= k && k <= 30); 21 | 22 | if(!( (a >> k) & 1) ) { 23 | printf("Even\n"); 24 | continue; 25 | } 26 | 27 | if(k == 0) { 28 | ll odd = (r - l + 1) / 2; 29 | if( (r - l + 1) % 2 == 1 && l % 2 == 1) 30 | odd++; 31 | 32 | if(odd % 2 == 1) 33 | printf("Odd\n"); 34 | else 35 | printf("Even\n"); 36 | 37 | continue; 38 | } 39 | else if(k == 1) { 40 | ll firstOdd = -1; 41 | for(ll i = l; i <= r; i++) { 42 | if(i % 2 == 1 && ( (i >> k) & 1)) { 43 | firstOdd = i; 44 | break; 45 | } 46 | } 47 | ll odd = (r - firstOdd) / 4 + 1; 48 | 49 | if(firstOdd == -1 || odd % 2 == 0) 50 | printf("Even\n"); 51 | else 52 | printf("Odd\n"); 53 | 54 | continue; 55 | } 56 | 57 | ll frst, start, stop; 58 | if((l >> k) & 1) 59 | frst = l; 60 | else 61 | frst = (l | (1 << k)) & (~( (1 << k) - 1) ); 62 | 63 | start = frst & (~( (1 << k) - 1) ); 64 | stop = start + (1 << k) - 1; 65 | if(stop >= r) { 66 | ll rng = (r - frst + 1); 67 | ll odd = rng / 2; 68 | if(rng % 2 == 1 && frst % 2 == 1) 69 | odd++; 70 | 71 | if(odd % 2 == 1) 72 | printf("Odd\n"); 73 | else 74 | printf("Even\n"); 75 | 76 | continue; 77 | } 78 | 79 | ll num1 = 0; 80 | ll rng = (stop - frst + 1); 81 | ll odd = rng / 2; 82 | if(rng % 2 == 1 && frst % 2 == 1) 83 | odd++; 84 | 85 | //cout << odd << " " << rng << endl; 86 | if(odd % 2 == 1) 87 | num1 = 1; 88 | 89 | if( !((r >> k) & 1) ) { 90 | if(num1) 91 | printf("Odd\n"); 92 | else 93 | printf("Even\n"); 94 | 95 | continue; 96 | } 97 | 98 | ll last = r; 99 | ll lastStart = last & (~( (1 << k) - 1) ); 100 | 101 | ll num2 = 0; 102 | rng = (last - lastStart + 1); 103 | odd = rng / 2; 104 | if(rng % 2 == 1 && lastStart % 2 == 1) 105 | odd++; 106 | 107 | if(odd % 2 == 1) 108 | num2 = 1; 109 | 110 | //cout << num1 << " " << num2 << endl; 111 | if((num1 + num2) % 2 == 0) 112 | printf("Even\n"); 113 | else 114 | printf("Odd\n"); 115 | } 116 | 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /G. Is Bita Happy?/Judge's Digit Dp Sol.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define ll long long 5 | #define pb push_back 6 | ll dp[2][2][34][34]; 7 | vector num; 8 | 9 | ll a, k, l, r; 10 | 11 | ll solve(ll isStart, ll isSmall, ll pos) 12 | 13 | { 14 | if(pos == 0) 15 | return 1; 16 | ll &ret = dp[isStart][isSmall][pos][k]; 17 | if(ret != -1 && isSmall) 18 | return ret; 19 | 20 | ll lim, pos2 = num.size() - pos; 21 | if(isSmall) 22 | lim = 1; 23 | else 24 | lim = num[pos2]; 25 | 26 | ll rt = 0; 27 | if(!isStart) { 28 | for(ll i = 0; i <= lim; i++) { 29 | if( (pos2 == num.size() - 1 || pos == k+1) && i == 0) 30 | continue; 31 | rt += solve(0, isSmall | i < num[pos2], pos - 1); 32 | } 33 | } 34 | else { 35 | if(pos <= k) 36 | return ret = 0; 37 | 38 | for(ll i = 1; i <= lim; i++) { 39 | if( (pos2 == num.size() - 1 || pos == k+1) && i == 0) 40 | continue; 41 | rt += solve(0, isSmall | i < num[pos2], pos - 1); 42 | } 43 | 44 | if(pos - 1 >= 1) 45 | rt += solve(1, 1, pos - 1); 46 | } 47 | 48 | return ret = rt; 49 | } 50 | 51 | ll calc(ll n) 52 | 53 | { 54 | if(n <= 0) 55 | return 0; 56 | 57 | ll tmp = n; 58 | num.clear(); 59 | while(tmp) { 60 | num.pb(tmp % 2); 61 | tmp /= 2; 62 | } 63 | reverse(num.begin(), num.end()); 64 | 65 | return solve(1, 0, num.size()); 66 | } 67 | 68 | int main() 69 | 70 | { 71 | ll t, caseno = 0; 72 | memset(dp, -1, sizeof(dp)); 73 | cin >> t; 74 | while(t--) { 75 | scanf("%lld %lld %lld %lld", &a, &k, &l, &r); 76 | 77 | if( !((a >> k) & 1) ) { 78 | printf("Even\n"); 79 | continue; 80 | } 81 | 82 | ll ans = calc(r); 83 | 84 | //cout << ans << endl; 85 | 86 | ans -= calc(l - 1); 87 | 88 | if(ans & 1) 89 | printf("Odd\n"); 90 | else 91 | printf("Even\n"); 92 | } 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /H. The Story of Stringland/Alter - reborn++.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef long long ll; 6 | 7 | namespace PalindromTree { 8 | const int N = 1e6 + 7; 9 | const int A = 26; 10 | 11 | int link[N], len[N], tree[N][A]; 12 | int idx, suff; 13 | string s; // 1 indexed 14 | 15 | void init(const string &str) { 16 | memset(link, 0, sizeof link); 17 | memset(len, 0, sizeof len); 18 | memset(tree, 0, sizeof tree); 19 | 20 | s = "#" + str; 21 | len[1] = -1, link[1] = 1; 22 | len[2] = 0, link[2] = 1; 23 | idx = suff = 2; 24 | } 25 | 26 | void add(int p) { 27 | while(s[p - len[suff] - 1] != s[p]) 28 | suff = link[suff]; 29 | int c = s[p] - 'a', x = link[suff]; 30 | while(s[p - len[x] - 1] != s[p]) 31 | x = link[x]; 32 | 33 | if(!tree[suff][c]) { 34 | tree[suff][c] = ++idx; 35 | len[idx] = len[suff] + 2; 36 | link[idx] = (len[idx] == 1) ? 2 : tree[x][c]; 37 | } 38 | suff = tree[suff][c]; 39 | } 40 | } 41 | 42 | namespace Hash { 43 | // String (double) hash. 44 | // - call precal() with maximum length possible. 45 | // - init(s) calculates h[] values for s 46 | // - hashval(l, r) for hash of s[l...r] 47 | 48 | const int N = 1e6 + 7; 49 | const ll P1 = 1e8 + 5e4 + 1; 50 | const ll P2 = 1e9 + 87; 51 | const ll MOD = 1e9 + 7; 52 | 53 | ll pow1[N], pow2[N]; 54 | ll h1[N], h2[N]; 55 | string s; 56 | 57 | void precal(int n) { 58 | pow1[0] = pow2[0] = 1; 59 | for(int i=1; i &p, const pair &q) { 85 | // cerr << "Comparing " << Hash::s.substr(p.first, p.second - p.first + 1) << " with " << Hash::s.substr(q.first, q.second - q.first + 1) << "\n"; 86 | int len = min(p.second - p.first + 1, q.second - q.first + 1); 87 | ll h1 = Hash::hashval(p.first, p.first + len - 1); 88 | ll h2 = Hash::hashval(q.first, q.first + len - 1); 89 | if(h1 == h2) { 90 | // cerr << "Same : " << (p.second - p.first + 1 < q.second - q.first + 1) << "\n"; 91 | if(p.second - p.first + 1 == q.second - q.first + 1) return p < q; 92 | return (p.second - p.first + 1 < q.second - q.first + 1); 93 | } 94 | 95 | int lo = 0, hi = len; 96 | while(lo < hi) { 97 | int mid = (lo + hi) / 2; 98 | h1 = Hash::hashval(p.first, p.first + mid - 1); 99 | h2 = Hash::hashval(q.first, q.first + mid - 1); 100 | if(h1 == h2) lo = mid + 1; 101 | else hi = mid; 102 | } 103 | // cerr << "Comp: " << (Hash::s[p.first+hi-1] < Hash::s[q.first+hi-1]) << " for " << Hash::s[p.first+hi-1] << " and " << Hash::s[q.first+hi-1] << "\n"; 104 | return Hash::s[p.first+hi-1] < Hash::s[q.first+hi-1]; 105 | } 106 | 107 | inline bool is_equal(const pair &p, const pair &q) { 108 | if(p.second - p.first + 1 != q.second - q.first + 1) return false; 109 | ll h1 = Hash::hashval(p.first, p.second); 110 | ll h2 = Hash::hashval(q.first, q.second); 111 | return h1 == h2; 112 | } 113 | 114 | int main() { 115 | ios::sync_with_stdio(false); 116 | cin.tie(0); cout.tie(0); 117 | 118 | string s; 119 | cin >> s; 120 | 121 | PalindromTree::init(s); 122 | 123 | int n = (int) s.size(); 124 | s = "#" + s; 125 | 126 | vector< pair > pals; 127 | for(int i=1; i<=n; ++i) { 128 | PalindromTree::add(i); 129 | int len = PalindromTree::len[PalindromTree::suff]; 130 | pals.push_back(make_pair(i-len+1, i)); 131 | // cout << "Palindrom: " << i-len+1 << " - " << i << " [ " << s.substr(i-len, len) << " ]" << "\n"; 132 | } 133 | 134 | Hash::precal(Hash::N); 135 | Hash::init(s); 136 | 137 | sort(pals.begin(), pals.end(), cmp); 138 | // for(auto &qq : pals) { 139 | // cerr << qq.first << " - " << qq.second << " [ " << s.substr(qq.first, qq.second - qq.first + 1) << " ]\n"; 140 | // } 141 | pals.erase(unique(pals.begin(), pals.end(), is_equal), pals.end()); 142 | // cerr << "Uniques: \n"; 143 | // for(auto &qq : pals) { 144 | // cerr << qq.first << " - " << qq.second << " [ " << s.substr(qq.first, qq.second - qq.first + 1) << " ]\n"; 145 | // } 146 | 147 | int q; 148 | cin >> q; 149 | 150 | while(q--) { 151 | int pos; 152 | cin >> pos; 153 | --pos; 154 | 155 | if(pos >= (int) pals.size()) { 156 | cout << "-1\n"; 157 | } 158 | else { 159 | cout << pals[pos].first << " " << pals[pos].second << "\n"; 160 | } 161 | } 162 | 163 | return 0; 164 | } 165 | -------------------------------------------------------------------------------- /H. The Story of Stringland/Sadman-01.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | const int MX=1000006; 4 | char s[MX]; 5 | int n,size,last; 6 | int p[MX],pn[MX],c[20][MX],cn[MX],cnt[MX]; 7 | struct sub{ 8 | int i,j; 9 | bool operator < (const sub &d) const 10 | { 11 | int x=i,y=j,p=d.i,q=d.j; 12 | if(y-x a={c[k][x],c[k][(x+l-(1< b={c[k][p],c[k][(p+l-(1<=0;i--) p[--cnt[c[k][pn[i]]]]=pn[i]; 44 | classes=1,cn[p[0]]=0; 45 | pair x,y; 46 | for(int i=1;i=0 && s[i]==s[j]) return n; 74 | n=pt[n].link; 75 | } 76 | return n; 77 | } 78 | void extend(char s[],int i) 79 | { 80 | int c=s[i]-97; 81 | int n=find(last,s,i); 82 | if(pt[n].to[c]==-1){ 83 | int m=size++; 84 | init_node(m); 85 | pt[n].to[c]=m; 86 | pt[m].len=pt[n].len+2; 87 | pt[m].pos=i-pt[m].len+1; 88 | if(pt[m].len==1) pt[m].link=1; 89 | else{ 90 | int v=find(pt[n].link,s,i); 91 | pt[m].link=pt[v].to[c]; 92 | } 93 | last=m; 94 | } 95 | else last=pt[n].to[c]; 96 | } 97 | int main() 98 | { 99 | int q,k; 100 | scanf("%s %d",s,&q); 101 | n=strlen(s); 102 | s[n]='$'; 103 | sort_cyclic_shifts(s,n+1); 104 | init_pt(); 105 | for(int i=0;im) printf("-1\n"); 116 | else printf("%d %d\n",st[k].i+1,st[k].j+1); 117 | } 118 | return 0; 119 | } 120 | -------------------------------------------------------------------------------- /H. The Story of Stringland/Shefin's Sol (Hash).cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace __gnu_pbds; 4 | using namespace std; 5 | 6 | #define ll long long 7 | #define LL unsigned long long 8 | #define base 73 9 | 10 | #define MAX_N 1000020 11 | 12 | LL fh[MAX_N], rh[MAX_N], pw[MAX_N]; 13 | gp_hash_table uniquePal; 14 | char s[MAX_N]; 15 | ll len, tot = 0; 16 | 17 | LL fHash(ll left, ll right) { 18 | return left==0? fh[right] : fh[right] - fh[left - 1] * pw[right-left+1]; 19 | } 20 | 21 | LL rHash(ll &left, ll &right) { 22 | return right==len-1? rh[left] : rh[left] - rh[right + 1] * pw[right-left+1]; 23 | } 24 | 25 | bool isPal(ll &left, ll &right) { 26 | return fHash(left, right) == rHash(left, right); 27 | } 28 | 29 | struct palrange { 30 | ll left, right; 31 | } ans[MAX_N]; 32 | 33 | bool cmp(const palrange &a, const palrange &b) { 34 | 35 | ll lo = 1, hi = min(a.right-a.left+1, b.right-b.left+1), firstDiff = -1; 36 | while(lo <= hi) { 37 | ll mid = (lo + hi) / 2; 38 | 39 | if( fHash(a.left, a.left+mid-1) != fHash(b.left, b.left+mid-1)) { 40 | firstDiff = mid; 41 | hi = mid - 1; 42 | } 43 | else 44 | lo = mid + 1; 45 | } 46 | 47 | if(firstDiff == -1) 48 | return (a.right-a.left+1) < (b.right-b.left+1); 49 | 50 | return s[a.left+firstDiff-1] < s[b.left+firstDiff-1]; 51 | } 52 | 53 | void oddPalGen(ll &i) { 54 | 55 | ll lo = 0, hi = min(i, len-1 - i), start; 56 | while(lo <= hi) { 57 | ll mid = (lo + hi) / 2; 58 | ll left = i - mid, right = i + mid; 59 | 60 | if(isPal(left, right)) { 61 | lo = mid + 1; 62 | start = left; 63 | } 64 | else 65 | hi = mid - 1; 66 | } 67 | 68 | ll nw = -1; 69 | lo = 0, hi = i - start; 70 | while(lo <= hi) { 71 | ll mid = (lo + hi) / 2; 72 | ll left = i - mid, right = i + mid; 73 | 74 | LL h = fHash(left, right); 75 | if(uniquePal.find(h) != uniquePal.end()) 76 | lo = mid + 1; 77 | else { 78 | nw = left; 79 | hi = mid - 1; 80 | } 81 | } 82 | if(nw == -1) 83 | return; 84 | 85 | for(ll j = nw; j >= start; j--) { 86 | ll right = i + (i - j); 87 | uniquePal[ fHash(j, right) ] = 1; 88 | ans[tot++] = {j, right}; 89 | if(tot >= 1000000) return; 90 | } 91 | } 92 | 93 | void evenPalGen(ll &i) { 94 | 95 | ll lo = 1, hi = min(i+1, len-1 - i), start = -1; 96 | while(lo <= hi) { 97 | ll mid = (lo + hi) / 2; 98 | ll left = i - mid + 1, right = i + mid; 99 | 100 | if(isPal(left, right)) { 101 | lo = mid + 1; 102 | start = left; 103 | } 104 | else 105 | hi = mid - 1; 106 | } 107 | if(start == -1) 108 | return; 109 | 110 | ll nw = -1; 111 | lo = 1, hi = i - start + 1; 112 | while(lo <= hi) { 113 | ll mid = (lo + hi) / 2; 114 | ll left = i - mid + 1, right = i + mid; 115 | 116 | LL h = fHash(left, right); 117 | if(uniquePal.find(h) != uniquePal.end()) 118 | lo = mid + 1; 119 | else { 120 | nw = left; 121 | hi = mid - 1; 122 | } 123 | } 124 | if(nw == -1) 125 | return; 126 | 127 | for(ll j = nw; j >= start; j--) { 128 | ll right = i + (i - j + 1); 129 | uniquePal[ fHash(j, right) ] = 1; 130 | ans[tot++] = {j, right}; 131 | if(tot >= 1000000) return; 132 | } 133 | } 134 | 135 | int main() 136 | 137 | { 138 | scanf("%s", s); 139 | len = strlen(s); 140 | assert(1 <= len && len <= 1000000); 141 | for(ll i = 0; s[i] != '\0'; i++) 142 | assert(s[i] >= 'a' && s[i] <= 'z'); 143 | 144 | pw[0] = 1; 145 | for(ll i = 1; i < MAX_N; i++) 146 | pw[i] = (pw[i-1] * base); 147 | 148 | for(ll i = 0; s[i] != '\0'; i++) { 149 | if(i == 0) 150 | fh[i] = (s[i] - 'a' + 1); 151 | else 152 | fh[i] = (fh[i-1] * base) + (s[i] - 'a' + 1); 153 | } 154 | for(ll i = len-1; i >= 0; i--) { 155 | if(i == len-1) 156 | rh[i] = (s[i] - 'a' + 1); 157 | else 158 | rh[i] = (rh[i+1] * base) + (s[i] - 'a' + 1); 159 | } 160 | 161 | for(ll i = 0; i < len; i++) { 162 | oddPalGen(i); 163 | if(tot >= 1000000) break; 164 | evenPalGen(i); 165 | if(tot >= 1000000) break; 166 | } 167 | 168 | sort(ans, ans + tot, cmp); 169 | 170 | ll q; 171 | cin >> q; 172 | assert(1 <= q && q <= 100000); 173 | while(q--) { 174 | ll k; 175 | scanf("%lld", &k); 176 | assert(1 <= k && k <= 1000000); 177 | if(tot < k) { 178 | printf("-1\n"); 179 | continue; 180 | } 181 | 182 | printf("%lld %lld\n", ans[k-1].left + 1, ans[k-1].right + 1); 183 | } 184 | 185 | string tmp; 186 | cin >> tmp; 187 | assert(!tmp.length()); 188 | 189 | return 0; 190 | } 191 | -------------------------------------------------------------------------------- /I. Solve This Giveaway Problem First/Judge Solution ( Hasinur_ ).cc: -------------------------------------------------------------------------------- 1 | #include 2 | #define LL long long 3 | #define ll long long 4 | #define SL(n) scanf("%lld",&n) 5 | #define LF double 6 | #define SF(n) scanf("%lf",&n) 7 | 8 | #define pb push_back 9 | #define ff first 10 | #define ss second 11 | #define mp make_pair 12 | 13 | #define ON(n,i) (n|(1<>1); 56 | a = ( a * a ) % m; 57 | } 58 | return r; 59 | } 60 | 61 | 62 | int main(){ 63 | 64 | //FILEIN; 65 | //FILEOUT; 66 | 67 | LL cs; 68 | SL(cs); 69 | FOR(T,1,cs){ 70 | 71 | 72 | scanf(" %s", N); 73 | LL P; 74 | SL(P); 75 | 76 | LL S; 77 | SL(S); 78 | 79 | for( LL i = 0 ; i 2 | using namespace std; 3 | 4 | #define ll long long 5 | const ll mod = 1e9 + 7; 6 | 7 | string n; 8 | ll p, num[6], l, n2, np; 9 | 10 | ll fastPow(ll x, ll n, ll MOD) 11 | { 12 | ll ret = 1; 13 | while (n) 14 | { 15 | if (n & 1) ret = (ret * x) % MOD; 16 | x = (x * x) % MOD; 17 | n >>= 1; 18 | } 19 | return ret % MOD; 20 | } 21 | 22 | ll calc(ll a) 23 | 24 | { 25 | ll rem = 0; 26 | for(ll i = 0; i < n.length(); i++) { 27 | ll now = n[i] - '0'; 28 | rem = (rem * 10 + now) % a; 29 | } 30 | rem = fastPow(rem, p, a); 31 | 32 | ll nom = ( (np - rem) % mod + mod) % mod; 33 | ll inva = fastPow(a, mod - 2, mod); 34 | 35 | return (nom * inva) % mod; 36 | } 37 | 38 | int main() 39 | 40 | { 41 | std::ios_base::sync_with_stdio(false); 42 | cin.tie(NULL), cout.tie(NULL); 43 | 44 | ll t, cs = 0, sumOfN = 0, mulOfL = 1; 45 | cin >> t; 46 | assert(1 <= t && t <= (ll)1e5); 47 | while(t--) { 48 | cin >> n >> p >> l; 49 | sumOfN += n.length(); 50 | assert(1 <= n.length()); 51 | assert(1 <= p && p <= (ll)1e5); 52 | assert(1 <= l && l <= 5); 53 | 54 | mulOfL = 1; 55 | for(ll i = 0; i < l; i++) { 56 | cin >> num[i]; 57 | assert(1 <= num[i] <= (ll)1e9); 58 | mulOfL *= num[i]; 59 | } 60 | 61 | n2 = 0; 62 | for(ll i = 0; i < n.length(); i++) { 63 | ll now = n[i] - '0'; 64 | n2 = (n2 * 10 + now) % mod; 65 | } 66 | np = fastPow(n2, p, mod); 67 | 68 | ll lim = (1 << l) - 1; 69 | ll ans = 0; 70 | for(ll i = 1; i <= lim; i++) { 71 | 72 | ll lcm = 1, cnt = 0; 73 | for(ll j = 0; j < l; j++) { 74 | if( !((i >> j) & 1) ) 75 | continue; 76 | 77 | ll g = __gcd(lcm, num[j]); 78 | lcm = (lcm * num[j]) / g; 79 | cnt++; 80 | } 81 | 82 | ll tot = calc(lcm); 83 | if(cnt & 1) 84 | ans = (ans + tot) % mod; 85 | else 86 | ans = (ans - tot) % mod; 87 | 88 | if(ans < 0) ans += mod; 89 | } 90 | 91 | cout << "Case " << ++cs << ": " << ans << endl; 92 | assert(1 <= mulOfL && mulOfL <= (ll)1e9); 93 | } 94 | 95 | assert(1 <= sumOfN && sumOfN <= (ll)1e6); 96 | 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /I. Solve This Giveaway Problem First/alter-mainstring.java: -------------------------------------------------------------------------------- 1 | import java.math.BigInteger; 2 | import java.util.ArrayList; 3 | import java.util.BitSet; 4 | import java.util.List; 5 | import java.util.Scanner; 6 | 7 | public class Unnamed { 8 | 9 | private static long p; 10 | private static long mod = 1000000007; 11 | private static final BigInteger TWO = BigInteger.ONE.add(BigInteger.ONE); 12 | private static Pair BASE_PAIR; 13 | 14 | private static Pair mul(Pair p1, Pair p2, long l) { 15 | 16 | long first = p1.getKey()*p2.getKey(); 17 | first %= mod; 18 | first *= l; 19 | first %= mod; 20 | first += p1.getKey()*p2.getValue(); 21 | first %= mod; 22 | first += p2.getKey()*p1.getValue(); 23 | first %= mod; 24 | first += (p1.getValue()*p2.getValue())/l; 25 | first %= mod; 26 | 27 | long second = (p1.getValue()*p2.getValue())%l; 28 | return new Pair<>(first, second); 29 | } 30 | 31 | private static Pair div(long d, long l) { 32 | if(d == 1) { 33 | return BASE_PAIR; 34 | } 35 | 36 | Pair p1, p2; 37 | p1 = div(d/2, l); 38 | p2 = mul(p1, p1, l); 39 | if(d%2 == 1) { 40 | p2 = mul(p2, BASE_PAIR, l); 41 | } 42 | return p2; 43 | } 44 | 45 | private static long gcd(long a, long b) { 46 | if(b>a) return gcd(b,a); 47 | if(b == 0) return a; 48 | return gcd(b, a%b); 49 | } 50 | 51 | private static long LCM(long a, long b) { 52 | return (a*b)/gcd(a,b); 53 | } 54 | 55 | public static void main(String[] args) { 56 | 57 | Scanner sc = new Scanner(System.in); 58 | BigInteger lim = BigInteger.TEN.pow(100000); 59 | 60 | int t = sc.nextInt(); 61 | assert (t>=1 && t<=100000); 62 | int cs = 1; 63 | while (t-- > 0) { 64 | 65 | BigInteger n = sc.nextBigInteger(); 66 | p = sc.nextLong(); 67 | 68 | assert (n.compareTo(BigInteger.ONE)>=0 && n.compareTo(lim)<=0); 69 | assert (p>=1 && p<=100000); 70 | 71 | int s = sc.nextInt(); 72 | assert (s>=1 && s<=5); 73 | List list = new ArrayList<>(); 74 | 75 | for(int i=0; i=1 && l<=1000000000); 78 | list.add(l); 79 | } 80 | 81 | long ans = 0; 82 | 83 | for(int i=1; i<(1<(temp[0].mod(BigInteger.valueOf(mod)).longValue(), 92 | temp[1].mod(BigInteger.valueOf(mod)).longValue()); 93 | if(set.cardinality()%2==0) { 94 | ans -= div(p, lcm).getKey(); 95 | } else { 96 | ans += div(p, lcm).getKey(); 97 | } 98 | ans %= mod; 99 | } 100 | if(ans< 0) ans+= mod; 101 | System.out.println("Case " + cs + ": " + ans); 102 | cs++; 103 | } 104 | } 105 | 106 | 107 | private static class Pair { 108 | 109 | private L first; 110 | private K second; 111 | 112 | private Pair(L first, K second) { 113 | this.first = first; 114 | this.second = second; 115 | } 116 | 117 | private L getKey() { 118 | return first; 119 | } 120 | private K getValue() { 121 | return second; 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /J. Let's See!/Alter - reborn++.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int honey_bunny(int x) { 6 | int ret = 0; 7 | for(int i=1; i<=x; ++i) ret += x % i; 8 | return ret; 9 | } 10 | 11 | void print_honey(int x) { 12 | cout << x << ":"; 13 | for(int i=1; i<=x; ++i) cout << " " << (x % i); 14 | cout << "\n"; 15 | } 16 | 17 | void precal() { 18 | static const int N = 1000; 19 | 20 | for(int i=1; i<=N; ++i) { 21 | if(honey_bunny(i) == honey_bunny(i-1)) { 22 | // cout << i << " - " << honey_bunny(i) << "\n"; 23 | print_honey(i-1); 24 | print_honey(i); 25 | cout << "\n"; 26 | } 27 | } 28 | } 29 | 30 | typedef long long ll; 31 | const int TC_MAX = 100000; 32 | const ll LR_MAX = 1000000000000000000LL; 33 | 34 | int main() { 35 | ios::sync_with_stdio(false); 36 | cin.tie(0); cout.tie(0); 37 | 38 | int t, tc = 0; 39 | assert(cin >> t); 40 | assert(t >= 1 and t <= TC_MAX); 41 | 42 | while(t--) { 43 | ll fr, to; 44 | assert(cin >> fr >> to); 45 | // if(fr > to) swap(fr, to); 46 | assert(fr <= to); // oh man, not this again 47 | assert(fr >= 1 and fr <= LR_MAX); 48 | assert(to >= 1 and to <= LR_MAX); 49 | 50 | ll res = 0; 51 | for(ll p2 = 1; p2 <= LR_MAX; p2 <<= 1) { 52 | if(p2 >= fr and p2 <= to) { 53 | res += p2; 54 | } 55 | } 56 | cout << res << "\n"; 57 | } 58 | 59 | string temp; 60 | assert(!(cin >> temp)); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /J. Let's See!/Alter(Hasinur_).cc: -------------------------------------------------------------------------------- 1 | #include 2 | #define LL long long 3 | #define ll long long 4 | #define SL(n) scanf("%lld",&n) 5 | #define LF double 6 | #define SF(n) scanf("%lf",&n) 7 | 8 | #define pb push_back 9 | #define ff first 10 | #define ss second 11 | #define mp make_pair 12 | 13 | #define ON(n,i) (n|(1<=L ) ans+= (unsigned LL)i; 47 | printf("%llu\n", ans); 48 | } 49 | 50 | 51 | return 0;} 52 | -------------------------------------------------------------------------------- /J. Let's See!/Judge Solution.cc: -------------------------------------------------------------------------------- 1 | #include ` 2 | 3 | #define slld(longvalue) scanf("%lld", &longvalue) 4 | #define plld(longvalue) printf("%lld\n", longvalue) 5 | 6 | #define slf(longvalue) scanf("%lf", &longvalue) 7 | #define plf(longvalue) printf("%lf\n", longvalue) 8 | #define sc(letter) scanf("%c", &letter) 9 | #define pc(letter) printf("%c", letter) 10 | 11 | #define ss(name) scanf("%s", name) 12 | #define ps(name) printf("%s", name) 13 | 14 | #define pnew printf("\n") 15 | 16 | #define ll long long 17 | #define ull unsigned long long 18 | #define pll pair < long long, long long > 19 | #define pii pair < int, int > 20 | 21 | #define printcase(indexing,ans) printf("Case %lld: %lld\n", indexing, ans) 22 | 23 | #define pb(x) push_back(x) 24 | 25 | #define bug printf("BUG\n") 26 | 27 | #define mxlld LLONG_MAX 28 | #define mnlld -LLONG_MAX 29 | 30 | #define mxd 2e8 31 | #define mnd -2e8 32 | 33 | #define pi 3.14159265359 34 | 35 | #define mod 1000000009 36 | 37 | 38 | using namespace std; 39 | 40 | bool check(ll n, ll pos) 41 | { 42 | return n & (1LL << pos); 43 | } 44 | 45 | ll Set(ll n, ll pos) 46 | { 47 | return n = n | (1LL << pos); 48 | } 49 | 50 | int main() 51 | { 52 | ll i, j, k, l, m, n, o, r, q; 53 | ll testcase; 54 | ll input, flag, tag, ans; 55 | 56 | // freopen("input.txt", "r", stdin); 57 | // freopen("output.txt", "w", stdout); 58 | 59 | cin >> testcase; 60 | 61 | for(j = 1; j <= testcase; j++) 62 | { 63 | ll temp = 1; 64 | 65 | ans = 0; 66 | 67 | cin >> l >> r; 68 | 69 | if(l > r) swap(l, r); 70 | 71 | for(i = 1; i <= 64; i++) 72 | { 73 | if(temp > r) break; 74 | 75 | if(temp >= l) ans += temp; 76 | 77 | temp *= 2; 78 | } 79 | 80 | cout << ans << endl; 81 | 82 | } 83 | 84 | 85 | } 86 | 87 | 88 | -------------------------------------------------------------------------------- /K. Happy Sub-Sequence/Alter-Hasnaine (Constraint Check).cc: -------------------------------------------------------------------------------- 1 | #include ` 2 | 3 | #define slld(longvalue) scanf("%lld", &longvalue) 4 | #define plld(longvalue) printf("%lld\n", longvalue) 5 | 6 | #define slf(longvalue) scanf("%lf", &longvalue) 7 | #define plf(longvalue) printf("%lf\n", longvalue) 8 | #define sc(letter) scanf("%c", &letter) 9 | #define pc(letter) printf("%c", letter) 10 | 11 | #define ss(name) scanf("%s", name) 12 | #define ps(name) printf("%s", name) 13 | 14 | #define pnew printf("\n") 15 | 16 | #define ll long long 17 | #define ull unsigned long long 18 | #define pll pair < long long, long long > 19 | #define pii pair < int, int > 20 | 21 | #define printcase(indexing,ans) printf("Case %lld: %lld\n", indexing, ans) 22 | 23 | #define pb(x) push_back(x) 24 | 25 | #define bug printf("BUG\n") 26 | 27 | #define mxlld LLONG_MAX 28 | #define mnlld -LLONG_MAX 29 | 30 | #define mxd 2e8 31 | #define mnd -2e8 32 | 33 | #define pi 3.14159265359 34 | 35 | #define mod 1000000007 36 | 37 | 38 | using namespace std; 39 | 40 | bool check(ll n, ll pos) 41 | { 42 | return n & (1LL << pos); 43 | } 44 | 45 | ll Set(ll n, ll pos) 46 | { 47 | return n = n | (1LL << pos); 48 | } 49 | 50 | ll arr[100005]; 51 | 52 | ll dp[1000006]; 53 | pll prevdp[1000006]; 54 | int main() 55 | { 56 | ll i, j, k, l, m, n, o, r, q; 57 | ll testcase; 58 | ll input, flag, tag, ans; 59 | 60 | // freopen("input.txt", "r", stdin); 61 | 62 | // freopen("output.txt", "w", stdout); 63 | 64 | slld(n); 65 | 66 | assert(1 <= n && n <= 100000); 67 | 68 | for(i = 1; i <= n; i++) 69 | { 70 | slld(arr[i]); 71 | 72 | assert(1 <= arr[i] && arr[i] <= 1000000); 73 | } 74 | 75 | ans = 0; 76 | 77 | for(i = 1; i <= n; i++) 78 | { 79 | dp[0] = 1; 80 | for(j = 1; j * j <= arr[i]; j++) 81 | { 82 | if(arr[i] % j) continue; 83 | 84 | ll y = arr[i] / j; 85 | ll x = j; 86 | 87 | prevdp[x].first = dp[x]; 88 | prevdp[x].second = i; 89 | 90 | if(prevdp[x - 1].second == i) 91 | { 92 | dp[x] += prevdp[x - 1].first % mod; 93 | dp[x] %= mod; 94 | } 95 | else 96 | { 97 | dp[x] += dp[x - 1] % mod; 98 | 99 | dp[x] %= mod; 100 | } 101 | 102 | if(x == y) continue; 103 | 104 | prevdp[y].first = dp[y]; 105 | prevdp[y].second = i; 106 | 107 | if(prevdp[y - 1].second == i) 108 | { 109 | dp[y] = (dp[y] + prevdp[y - 1].first) % mod; 110 | } 111 | else 112 | { 113 | dp[y] = (dp[y] + dp[y - 1]) % mod; 114 | } 115 | 116 | } 117 | 118 | // cout << dp[1] << " " << dp[2] << endl; 119 | } 120 | 121 | for(i = 1; i <= 1000000; i++) 122 | { 123 | ans += dp[i] % mod; 124 | 125 | ans %= mod; 126 | } 127 | 128 | cout << ans << endl; 129 | 130 | } 131 | 132 | 133 | -------------------------------------------------------------------------------- /K. Happy Sub-Sequence/Judge Solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | /***template***/ 6 | #define ll long long 7 | #define dd double 8 | #define scl(n) scanf("%lld",&n) 9 | #define scd(n) scanf("%lf",&n) 10 | #define pi pair 11 | #define pb push_back 12 | #define mp make_pair 13 | #define uu first 14 | #define vv second 15 | #define FOR(i,n) for(ll i=1;i<=n;i++) 16 | #define LOOP(i,n) for(ll i=0;i, greater >q 30 | 31 | 32 | vectorprime[1000006]; 33 | ll cnt[1000005]; 34 | 35 | ll ara[100005]; 36 | 37 | ll n, ans = 0; 38 | 39 | int main() 40 | { 41 | FOR(i,1000001) 42 | { 43 | for(ll j = i; j <= 1000001; j += i) 44 | prime[j].push_back(i); 45 | } 46 | 47 | scl(n); 48 | 49 | LOOP(i,n) 50 | { 51 | scl(ara[i]); 52 | 53 | assert(1 <= ara[i] && ara[i] <= 1000000); 54 | } 55 | cnt[0] = 1; 56 | 57 | LOOP(i, n + 1) 58 | { 59 | ll sz = prime[ ara[i] ].size(); 60 | for(ll j = sz - 1; j >= 0; j--) 61 | { 62 | ll now = prime[ ara[i] ][j]; 63 | ans = (ans + cnt[now - 1]) % mod; 64 | cnt[now] =(cnt[now] + cnt[now - 1]) % mod; 65 | } 66 | } 67 | 68 | printf("%lld\n",ans); 69 | } 70 | -------------------------------------------------------------------------------- /K. Happy Sub-Sequence/alter - mainstring.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | import java.util.Scanner; 4 | 5 | import static java.lang.Math.min; 6 | 7 | public class Unnamed { 8 | 9 | private static final int mod = 1000000007; 10 | 11 | public static void main(String[] args) { 12 | 13 | Scanner sc = new Scanner(System.in); 14 | int n = sc.nextInt(); 15 | assert (n>=1 && n<=100000); 16 | List ar = new ArrayList<>(); 17 | List dp = new ArrayList<>(); 18 | // List e = new ArrayList<>(); 19 | 20 | for(int i=0; i=1 && x<=1000000); 25 | // e.add(0); 26 | } 27 | dp.add(0); 28 | dp.add(0); 29 | // e.add(0); 30 | // e.add(0); 31 | 32 | for(int i=n-1; i>=0; i--) { 33 | int sq = (int) Math.sqrt(ar.get(i)); 34 | sq++; 35 | if(sq*sq > ar.get(i)) sq--; 36 | 37 | for(int j=1; j<=n && j<=sq; j++) { 38 | if(ar.get(i)%j == 0) { 39 | // e.set(j, e.get(j)+1); 40 | dp.set(j, (dp.get(j+1)+dp.get(j)+1)%mod); 41 | // System.out.println(i + " " + j + " " + e.get(j) + " " + dp.get(j)); 42 | } 43 | } 44 | for(int j= sq; j>0; j--) { 45 | if(ar.get(i)%j == 0) { 46 | int k = ar.get(i)/j; 47 | if(k>n || k == j) continue; 48 | // e.set(k, e.get(k)+1); 49 | dp.set(k, (dp.get(k+1)+dp.get(k) +1)%mod); 50 | // System.out.println(i + " " + k + " " + e.get(k) + " " + dp.get(k)); 51 | } 52 | } 53 | } 54 | // System.out.println(dp.toString()); 55 | System.out.println(dp.get(1)); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /L. Information Retrieval/Alter - reborn++ - int id.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | map < string, string > synonym; 7 | map < string, int > id; 8 | 9 | inline int get_id(const string &s) { 10 | auto it = id.find(s); 11 | if(it != id.end()) return it->second; 12 | 13 | int sz = id.size(); 14 | id[s] = sz; 15 | 16 | auto it2 = synonym.find(s); 17 | if(it2 != synonym.end()) { 18 | id[it2->second] = sz; 19 | } 20 | 21 | return sz; 22 | } 23 | 24 | inline void to_low(string &s) { 25 | for(char &c : s) if(isalpha(c)) c = tolower(c); 26 | } 27 | 28 | vector process_line(const string &line) { 29 | vector ret; 30 | 31 | stringstream ss(line); 32 | string s; 33 | while(ss >> s) { 34 | ret.push_back(get_id(s)); 35 | } 36 | 37 | sort(ret.begin(), ret.end()); 38 | ret.erase(unique(ret.begin(), ret.end()), ret.end()); 39 | 40 | return ret; 41 | } 42 | 43 | const int SIMILAR_WORD_LINE = 120; 44 | const int QUERY_ANS_LINE = 6500; 45 | const int QUERY_LEN = 200; 46 | const int ANS_LEN = 800; 47 | 48 | int main() { 49 | ios::sync_with_stdio(false); 50 | cin.tie(0); cout.tie(0); 51 | 52 | string str; 53 | assert(cin >> str); 54 | assert(str == "Similar"); 55 | 56 | bool query_word = false; 57 | int similar_cnt = 0; 58 | 59 | while(cin >> str) { 60 | if(str == "Query") { 61 | query_word = true; 62 | break; 63 | } 64 | 65 | string syn; 66 | assert(cin >> syn); 67 | 68 | to_low(str); 69 | to_low(syn); 70 | synonym[str] = syn; 71 | synonym[syn] = str; 72 | 73 | get_id(str); 74 | get_id(syn); 75 | 76 | ++similar_cnt; 77 | } 78 | assert(query_word); 79 | assert(similar_cnt <= SIMILAR_WORD_LINE); 80 | 81 | assert(getline(cin, str)); // blank line 82 | assert(str.empty()); 83 | 84 | bool quit_word = false; 85 | int qa_cnt = 0; 86 | vector< vector > lhs, rhs; 87 | vector< string > qu, re; 88 | 89 | while(getline(cin, str)) { 90 | if(str == "QUIT") { 91 | quit_word = true; 92 | break; 93 | } 94 | 95 | assert(count(str.begin(), str.end(), '\t') == 1); 96 | int pos = str.find('\t'); 97 | assert(pos > 0); 98 | assert(pos + 1 < (int) str.size()); 99 | 100 | string t_qu = str.substr(0, pos); 101 | string t_re = str.substr(pos + 1); 102 | 103 | assert((int) t_qu.size() <= QUERY_LEN); 104 | assert((int) t_re.size() <= ANS_LEN); 105 | 106 | qu.push_back(t_qu); 107 | re.push_back(t_re); 108 | 109 | to_low(t_qu); 110 | to_low(t_re); 111 | 112 | lhs.push_back(process_line(t_qu)); 113 | rhs.push_back(process_line(t_re)); 114 | 115 | ++qa_cnt; 116 | } 117 | assert(quit_word); 118 | assert(qa_cnt <= QUERY_ANS_LINE); 119 | 120 | assert(qu.size() == re.size()); 121 | assert(qu.size() == lhs.size()); 122 | assert(re.size() == rhs.size()); 123 | 124 | // for(auto &qq : lhs) { 125 | // cerr << "Query:\n"; 126 | // for(auto &ss : qq) cerr << ss << " | "; 127 | // cerr << "\n"; 128 | // } 129 | // for(auto &qq : rhs) { 130 | // cerr << "Response:\n"; 131 | // for(auto &ss : qq) cerr << ss << " | "; 132 | // cerr << "\n"; 133 | // } 134 | 135 | for(int i=0; i<(int) qu.size(); ) { 136 | int fr = i, to = i; 137 | while(to < (int) qu.size() and qu[to] == qu[fr]) ++to; 138 | --to; 139 | 140 | vector< pair > vec; 141 | 142 | for(int j=fr; j<=to; ++j) { 143 | int cnt = 0; 144 | for(int lp=0, rp=0; lp<(int) lhs[i].size() and rp<(int) rhs[j].size(); ) { 145 | if(lhs[i][lp] == rhs[j][rp]) { 146 | ++cnt; 147 | ++lp; 148 | ++rp; 149 | } 150 | else if(lhs[i][lp] < rhs[j][rp]) { 151 | ++lp; 152 | } 153 | else { 154 | ++rp; 155 | } 156 | } 157 | 158 | vec.push_back(make_pair(-cnt, j)); 159 | } 160 | 161 | sort(vec.begin(), vec.end()); 162 | for(auto &qq : vec) { 163 | cout << qu[i] << "\t" << re[qq.second] << "\n"; 164 | } 165 | 166 | i = to + 1; 167 | } 168 | 169 | assert(!(cin >> str)); 170 | 171 | return 0; 172 | } 173 | -------------------------------------------------------------------------------- /L. Information Retrieval/Alter - reborn++.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | map < string, string > synonym; 7 | // map < string, int > id; 8 | 9 | // inline int get_id(const string &s) { 10 | // auto it = id.find(s); 11 | // if(it != id.end()) return it->second; 12 | 13 | // int sz = id.size(); 14 | // id[s] = sz; 15 | 16 | // auto it2 = synonym.find(s); 17 | // if(it2 != synonym.end()) { 18 | // id[it2->second] = sz; 19 | // } 20 | 21 | // return sz; 22 | // } 23 | 24 | inline void to_low(string &s) { 25 | for(char &c : s) if(isalpha(c)) c = tolower(c); 26 | } 27 | 28 | vector process_line(const string &line) { 29 | vector ret; 30 | 31 | stringstream ss(line); 32 | string s; 33 | while(ss >> s) { 34 | auto it = synonym.find(s); 35 | if(it != synonym.end()) { 36 | if(it->second < s) s = it->second; 37 | } 38 | ret.push_back(s); 39 | } 40 | 41 | sort(ret.begin(), ret.end()); 42 | ret.erase(unique(ret.begin(), ret.end()), ret.end()); 43 | 44 | return ret; 45 | } 46 | 47 | const int SIMILAR_WORD_LINE = 120; 48 | const int QUERY_ANS_LINE = 6500; 49 | const int QUERY_LEN = 200; 50 | const int ANS_LEN = 800; 51 | 52 | int main() { 53 | ios::sync_with_stdio(false); 54 | cin.tie(0); cout.tie(0); 55 | 56 | string str; 57 | assert(cin >> str); 58 | assert(str == "Similar"); 59 | 60 | bool query_word = false; 61 | int similar_cnt = 0; 62 | 63 | while(cin >> str) { 64 | if(str == "Query") { 65 | query_word = true; 66 | break; 67 | } 68 | 69 | string syn; 70 | assert(cin >> syn); 71 | 72 | to_low(str); 73 | to_low(syn); 74 | synonym[str] = syn; 75 | synonym[syn] = str; 76 | 77 | ++similar_cnt; 78 | } 79 | assert(query_word); 80 | assert(similar_cnt <= SIMILAR_WORD_LINE); 81 | 82 | assert(getline(cin, str)); // blank line 83 | assert(str.empty()); 84 | 85 | bool quit_word = false; 86 | int qa_cnt = 0; 87 | vector< vector< string > > lhs, rhs; 88 | vector< string > qu, re; 89 | 90 | while(getline(cin, str)) { 91 | if(str == "QUIT") { 92 | quit_word = true; 93 | break; 94 | } 95 | 96 | assert(count(str.begin(), str.end(), '\t') == 1); 97 | int pos = str.find('\t'); 98 | assert(pos > 0); 99 | assert(pos + 1 < (int) str.size()); 100 | 101 | string t_qu = str.substr(0, pos); 102 | string t_re = str.substr(pos + 1); 103 | 104 | assert((int) t_qu.size() <= QUERY_LEN); 105 | assert((int) t_re.size() <= ANS_LEN); 106 | 107 | qu.push_back(t_qu); 108 | re.push_back(t_re); 109 | 110 | to_low(t_qu); 111 | to_low(t_re); 112 | 113 | lhs.push_back(process_line(t_qu)); 114 | rhs.push_back(process_line(t_re)); 115 | 116 | ++qa_cnt; 117 | } 118 | assert(quit_word); 119 | assert(qa_cnt <= QUERY_ANS_LINE); 120 | 121 | assert(qu.size() == re.size()); 122 | assert(qu.size() == lhs.size()); 123 | assert(re.size() == rhs.size()); 124 | 125 | set < string > pre_lines; 126 | for(int i=0; i<(int) qu.size(); ++i) { 127 | string str = qu[i]; 128 | to_low(str); 129 | 130 | if(pre_lines.find(str) != pre_lines.end()) { 131 | assert(qu[i] == qu[i-1]); 132 | } 133 | 134 | pre_lines.insert(str); 135 | } 136 | pre_lines.clear(); 137 | 138 | // for(auto &qq : lhs) { 139 | // cerr << "Query:\n"; 140 | // for(auto &ss : qq) cerr << ss << " | "; 141 | // cerr << "\n"; 142 | // } 143 | // for(auto &qq : rhs) { 144 | // cerr << "Response:\n"; 145 | // for(auto &ss : qq) cerr << ss << " | "; 146 | // cerr << "\n"; 147 | // } 148 | 149 | for(int i=0; i<(int) qu.size(); ) { 150 | int fr = i, to = i; 151 | while(to < (int) qu.size() and qu[to] == qu[fr]) ++to; 152 | --to; 153 | 154 | vector< pair > vec; 155 | 156 | for(int j=fr; j<=to; ++j) { 157 | int cnt = 0; 158 | for(int lp=0, rp=0; lp<(int) lhs[i].size() and rp<(int) rhs[j].size(); ) { 159 | if(lhs[i][lp] == rhs[j][rp]) { 160 | ++cnt; 161 | ++lp; 162 | ++rp; 163 | } 164 | else if(lhs[i][lp] < rhs[j][rp]) { 165 | ++lp; 166 | } 167 | else { 168 | ++rp; 169 | } 170 | } 171 | 172 | vec.push_back(make_pair(-cnt, j)); 173 | } 174 | 175 | sort(vec.begin(), vec.end()); 176 | for(auto &qq : vec) { 177 | cout << qu[i] << "\t" << re[qq.second] << "\n"; 178 | } 179 | 180 | i = to + 1; 181 | } 182 | 183 | assert(!(cin >> str)); 184 | 185 | return 0; 186 | } 187 | -------------------------------------------------------------------------------- /L. Information Retrieval/CLown1331.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int sz = 1e5 + 10; 5 | using ll = long long; 6 | 7 | struct Query { 8 | string QueryString; 9 | string CandidateResult; 10 | int Relevancy; 11 | int QueryGroup; 12 | int Order; 13 | }; 14 | 15 | vector split(const string &s, char splitChar) { 16 | vector splittedString; 17 | string hand = ""; 18 | for (char c: s) { 19 | if (c == splitChar) { 20 | splittedString.push_back(hand); 21 | hand.clear(); 22 | continue; 23 | } 24 | hand.push_back(c); 25 | } 26 | splittedString.push_back(hand); 27 | return splittedString; 28 | } 29 | 30 | unordered_map > simmilarDictionay; 31 | vector > simmilarPairs; 32 | unordered_map queryNumber; 33 | 34 | vector Queries; 35 | 36 | void makeLowerCaseString(vector &strList) { 37 | for_each(strList.begin(), strList.end(), [](string &s) -> void { 38 | for (char &c: s) { 39 | c = tolower(c); 40 | } 41 | }); 42 | } 43 | 44 | int calculateRelevancy(vector &queryString, vector &candidateResult) { 45 | makeLowerCaseString(queryString); 46 | makeLowerCaseString(candidateResult); 47 | int relevancy = 0; 48 | unordered_set candidateSet; 49 | unordered_set querySet; 50 | for (const string &s: queryString) { 51 | querySet.insert(s); 52 | } 53 | for (const string &s: candidateResult) { 54 | candidateSet.insert(s); 55 | } 56 | unordered_set relevantSet; 57 | for (const string &s: queryString) { 58 | if (candidateSet.find(s) != candidateSet.end()) { 59 | relevantSet.insert(s); 60 | } 61 | if (simmilarDictionay.count(s)) { 62 | for (const string &t: simmilarDictionay[s]) { 63 | if (candidateSet.find(t) != candidateSet.end() && querySet.find(t) == querySet.end()) { 64 | relevantSet.insert(s); 65 | } 66 | } 67 | } 68 | } 69 | for (const auto &it: simmilarPairs) { 70 | if (relevantSet.find(it.second) != relevantSet.end()) { 71 | relevantSet.erase(it.second); 72 | relevantSet.insert(it.first); 73 | } 74 | } 75 | return relevantSet.size(); 76 | } 77 | 78 | int main() { 79 | 80 | string line; 81 | 82 | getline(cin, line); 83 | 84 | while (getline(cin, line) && (line.size() != 5 && line != "Query")) { 85 | auto simmilarWords = split(line, ' '); 86 | makeLowerCaseString(simmilarWords); 87 | simmilarPairs.push_back(make_pair(simmilarWords[0], simmilarWords[1])); 88 | simmilarDictionay[simmilarWords[0]].insert(simmilarWords[1]); 89 | simmilarDictionay[simmilarWords[1]].insert(simmilarWords[0]); 90 | } 91 | 92 | int number = 0; 93 | 94 | int order = 0; 95 | 96 | while (getline(cin, line) && (line.size() != 4 && line != "QUIT")) { 97 | auto query = split(line, '\t'); 98 | auto queryString = split(query[0], ' '); 99 | auto candidateResult = split(query[1], ' '); 100 | if (queryNumber.count(query[0]) == 0) { 101 | queryNumber[query[0]] = ++number; 102 | } 103 | Queries.push_back({ 104 | query[0], 105 | query[1], 106 | calculateRelevancy(queryString, candidateResult), 107 | queryNumber[query[0]], 108 | ++order 109 | }); 110 | } 111 | 112 | sort(Queries.begin(), Queries.end(), [](const Query &a, const Query &b) -> bool { 113 | if (a.QueryGroup == b.QueryGroup) { 114 | if (a.Relevancy == b.Relevancy) { 115 | return a.Order < b.Order; 116 | } 117 | return a.Relevancy > b.Relevancy; 118 | } 119 | return a.QueryGroup < b.QueryGroup; 120 | }); 121 | 122 | for (Query query: Queries) { 123 | cout << query.QueryString << "\t" << query.CandidateResult << "\n"; 124 | } 125 | 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /L. Information Retrieval/Original Solution.py: -------------------------------------------------------------------------------- 1 | #dataset="LargeData1" 2 | #file=open(dataset+".txt") 3 | #output=open("SolLargeData.txt","w+") 4 | def Main(): 5 | flag=0 6 | res=[] 7 | sim={} 8 | file=[] 9 | while True: 10 | try: 11 | line = input() 12 | file.append(line) 13 | if(line=='QUIT'): 14 | break 15 | except EOFError: 16 | break 17 | flag=3 18 | for j in file: 19 | if(j=="Similar"): 20 | flag=1 21 | continue 22 | if(j=="Query"): 23 | flag=2 24 | continue 25 | if flag==1: 26 | r=j.split() 27 | if r[0] not in sim: 28 | sim[r[0].lower()]=r[1].lower() 29 | sim[r[1].lower()]=r[1].lower() 30 | continue 31 | if (j == "QUIT"): 32 | flag=3 33 | break 34 | sumval=0 35 | j = j.split('\t') 36 | set1=set() 37 | set2=set() 38 | o_x=j[0] 39 | o_y=j[1] 40 | x=j[0] 41 | y=j[1] 42 | x=x.lower() 43 | y=y.lower() 44 | x = " " + x + " " 45 | y = " " + y + " " 46 | for i in sim: 47 | 48 | x = x.replace(" "+i+" "," "+sim[i]+" ") 49 | y = y.replace(" "+i+" "," "+sim[i]+" ") 50 | #print(x) 51 | #print(y) 52 | x=x.split() 53 | y=y.split() 54 | #print(x) 55 | #print(y) 56 | for i in x: 57 | set1.add(i) 58 | for i in y: 59 | set2.add(i) 60 | intersect=set1.intersection(set2) 61 | sumval=len(intersect) 62 | p=' ' 63 | x=p.join(x) 64 | y=p.join(y) 65 | lines=[] 66 | lines.append(o_x) 67 | lines.append(o_y) 68 | lines.append(str(sumval)) 69 | res.append([lines[0], lines[1], int(lines[2])]) 70 | d=0 71 | newres=[] 72 | finalres=[] 73 | v=0 74 | 75 | for i in res: 76 | #print(i) 77 | 78 | v = v + 1 79 | #print(v) 80 | if(d==0): 81 | t=i[0] 82 | newres.append(i) 83 | d=d+1 84 | if (v == len(res)): 85 | finalres.extend(newres) 86 | continue 87 | tt=i[0] 88 | if(t!=tt): 89 | d=0 90 | newres.sort(key = lambda x:x[2],reverse=True) 91 | finalres.extend(newres) 92 | newres=[] 93 | newres.append(i) 94 | t=i[0] 95 | d=d+1 96 | if(v==len(res)): 97 | finalres.extend(newres) 98 | continue 99 | if (v == len(res)): 100 | newres.append(i) 101 | d = 0 102 | newres.sort(key=lambda x: x[2],reverse=True) 103 | finalres.extend(newres) 104 | newres = [] 105 | continue 106 | newres.append(i) 107 | d = d + 1 108 | output=open("temp.txt","w+") 109 | for i in finalres: 110 | x=i[0] 111 | y=i[1] 112 | print(x+"\t"+y) 113 | #output.write(x+"\t"+y+"\n") 114 | if __name__ == '__main__': 115 | Main() 116 | -------------------------------------------------------------------------------- /M. Ada Lovelace/Hasnaine alter in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int x = 'A' + 'd' + 'a'; 6 | printf("%d\n", x); 7 | } 8 | -------------------------------------------------------------------------------- /M. Ada Lovelace/Judge Solution.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // your code goes here 6 | string s = "Ada" ; 7 | 8 | int res = 0 ; 9 | 10 | for( int i = 0 ; i < s.size() ; i ++ ) { 11 | res += s[ i ] ; 12 | } 13 | 14 | cout << res << endl ; 15 | } 16 | -------------------------------------------------------------------------------- /M. Ada Lovelace/Python.py: -------------------------------------------------------------------------------- 1 | print(262) 2 | -------------------------------------------------------------------------------- /M. Ada Lovelace/Zeron alter in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | printf("%d\n", 'A' + 'd' + 'a'); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /M. Ada Lovelace/Zeron alter.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | cout << 'A' + 'd' + 'a' << endl; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /M. Ada Lovelace/c++.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | cout<<"262"; 6 | } 7 | -------------------------------------------------------------------------------- /NGPC2019 Analysis.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rebornplusplus/NGPC2019/f70ccb99b18490498786e879de157cc1ba7442ca/NGPC2019 Analysis.pdf -------------------------------------------------------------------------------- /NGPC2019 Problemset.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rebornplusplus/NGPC2019/f70ccb99b18490498786e879de157cc1ba7442ca/NGPC2019 Problemset.pdf -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ![](https://uploads.toph.co/arena-images/1570007152539447659-5792023559445812070-88b757133689ac02b9bafb12e73a7497.jpg) 2 | 3 | # NGPC 2019 4 | 5 | For time and memory constraints, look for the zip from toph 6 | -------------------------------------------------------------------------------- /toph-contest-ngpc-2019-problem-set.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rebornplusplus/NGPC2019/f70ccb99b18490498786e879de157cc1ba7442ca/toph-contest-ngpc-2019-problem-set.zip --------------------------------------------------------------------------------