{"problem":{"name":"D. Picking Strings","description":{"content":"Alice has a string consisting of characters '_A_', '_B_' and '_C_'. Bob can use the following transitions on any substring of our string in any order any number of times: *   _A_ _BC_ *   _B_ _AC_ * ","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF923D"},"statements":[{"statement_type":"Markdown","content":"Alice has a string consisting of characters '_A_', '_B_' and '_C_'. Bob can use the following transitions on any substring of our string in any order any number of times:\n\n*   _A_ _BC_\n*   _B_ _AC_\n*   _C_ _AB_\n*   _AAA_ empty string\n\nNote that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.\n\n## Input\n\nThe first line contains a string _S_ (1 ≤ |_S_| ≤ 105). The second line contains a string _T_ (1 ≤ |_T_| ≤ 105), each of these strings consists only of uppercase English letters '_A_', '_B_' and '_C_'.\n\nThe third line contains the number of queries _Q_ (1 ≤ _Q_ ≤ 105).\n\nThe following _Q_ lines describe queries. The _i_\\-th of these lines contains four space separated integers _a__i_, _b__i_, _c__i_, _d__i_. These represent the _i_\\-th query: is it possible to create _T_\\[_c__i_.._d__i_\\] from _S_\\[_a__i_.._b__i_\\] by applying the above transitions finite amount of times?\n\nHere, _U_\\[_x_.._y_\\] is a substring of _U_ that begins at index _x_ (indexed from 1) and ends at index _y_. In particular, _U_\\[1..|_U_|\\] is the whole string _U_.\n\nIt is guaranteed that 1 ≤ _a_ ≤ _b_ ≤ |_S_| and 1 ≤ _c_ ≤ _d_ ≤ |_T_|.\n\n## Output\n\nPrint a string of _Q_ characters, where the _i_\\-th character is '_1_' if the answer to the _i_\\-th query is positive, and '_0_' otherwise.\n\n[samples]\n\n## Note\n\nIn the first query we can achieve the result, for instance, by using transitions .\n\nThe third query asks for changing _AAB_ to _A_ — but in this case we are not able to get rid of the character '_B_'.","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"Alice 有一个由字符 '_A_'、'_B_' 和 '_C_' 组成的字符串。Bob 可以在字符串的任意子串上，以任意顺序任意次数应用以下变换：\n\n注意，子串是指一个或多个连续的字符。对于给定的查询，请判断是否能从源字符串得到目标字符串。\n\n第一行包含一个字符串 #cf_span[S] (#cf_span[1 ≤ |S| ≤ 105])。第二行包含一个字符串 #cf_span[T] (#cf_span[1 ≤ |T| ≤ 105])，这两个字符串仅由大写英文字母 '_A_'、'_B_' 和 '_C_' 组成。\n\n第三行包含查询数量 #cf_span[Q] (#cf_span[1 ≤ Q ≤ 105])。\n\n接下来的 #cf_span[Q] 行描述查询。第 #cf_span[i] 行包含四个用空格分隔的整数 #cf_span[ai]、#cf_span[bi]、#cf_span[ci]、#cf_span[di]，表示第 #cf_span[i] 个查询：是否可以通过有限次应用上述变换，将 #cf_span[S[ai..bi]] 变为 #cf_span[T[ci..di]]？\n\n这里，#cf_span[U[x..y]] 表示字符串 #cf_span[U] 中从索引 #cf_span[x]（从 1 开始计数）开始、到索引 #cf_span[y] 结束的子串。特别地，#cf_span[U[1..|U|]] 表示整个字符串 #cf_span[U]。\n\n保证 #cf_span[1 ≤ a ≤ b ≤ |S|] 且 #cf_span[1 ≤ c ≤ d ≤ |T|]。\n\n请输出一个长度为 #cf_span[Q] 的字符串，其中第 #cf_span[i] 个字符为 '_1_' 表示第 #cf_span[i] 个查询的答案为肯定，为 '_0_' 表示答案为否定。\n\n在第一个查询中，我们可以通过使用变换来实现目标结果。\n\n第三个查询要求将 _AAB_ 变为 _A_ —— 但在这种情况下，我们无法消除字符 '_B_'。\n\n## Input\n\n第一行包含一个字符串 #cf_span[S] (#cf_span[1 ≤ |S| ≤ 105])。第二行包含一个字符串 #cf_span[T] (#cf_span[1 ≤ |T| ≤ 105])，这两个字符串仅由大写英文字母 '_A_'、'_B_' 和 '_C_' 组成。第三行包含查询数量 #cf_span[Q] (#cf_span[1 ≤ Q ≤ 105])。接下来的 #cf_span[Q] 行描述查询。第 #cf_span[i] 行包含四个用空格分隔的整数 #cf_span[ai]、#cf_span[bi]、#cf_span[ci]、#cf_span[di]，表示第 #cf_span[i] 个查询：是否可以通过有限次应用上述变换，将 #cf_span[S[ai..bi]] 变为 #cf_span[T[ci..di]]？这里，#cf_span[U[x..y]] 表示字符串 #cf_span[U] 中从索引 #cf_span[x]（从 1 开始计数）开始、到索引 #cf_span[y] 结束的子串。特别地，#cf_span[U[1..|U|]] 表示整个字符串 #cf_span[U]。保证 #cf_span[1 ≤ a ≤ b ≤ |S|] 且 #cf_span[1 ≤ c ≤ d ≤ |T|]。\n\n## Output\n\n请输出一个长度为 #cf_span[Q] 的字符串，其中第 #cf_span[i] 个字符为 '_1_' 表示第 #cf_span[i] 个查询的答案为肯定，为 '_0_' 表示答案为否定。\n\n[samples]\n\n## Note\n\n在第一个查询中，我们可以通过使用变换来实现目标结果。第三个查询要求将 _AAB_ 变为 _A_ —— 但在这种情况下，我们无法消除字符 '_B_'。","is_translate":true,"language":"Chinese"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ S, T \\in \\{A, B, C\\}^* $ be the source and target strings.  \nLet $ Q \\in \\mathbb{Z}^+ $ be the number of queries.  \nFor each query $ i \\in \\{1, \\dots, Q\\} $, let $ [a_i, b_i] \\subseteq [1, |S|] $ and $ [c_i, d_i] \\subseteq [1, |T|] $ be intervals defining substrings $ S[a_i..b_i] $ and $ T[c_i..d_i] $.\n\n**Transitions**  \nThe allowed operations are:  \n- $ A \\leftrightarrow BC $  \n- $ B \\leftrightarrow CA $  \n- $ C \\leftrightarrow AB $  \n\nThese are reversible, and may be applied to any substring any number of times.\n\n**Constraints**  \n1. $ 1 \\le |S|, |T| \\le 10^5 $  \n2. $ 1 \\le Q \\le 10^5 $  \n3. For each query: $ 1 \\le a_i \\le b_i \\le |S| $, $ 1 \\le c_i \\le d_i \\le |T| $\n\n**Objective**  \nFor each query $ i $, determine whether $ S[a_i..b_i] $ can be transformed into $ T[c_i..d_i] $ using the given transitions.  \nOutput a string of length $ Q $, where the $ i $-th character is `'1'` if possible, `'0'` otherwise.\n\n**Key Invariant**  \nDefine the weight function $ w: \\{A, B, C\\} \\to \\mathbb{Z}/3\\mathbb{Z} $ by $ w(A) = 1 $, $ w(B) = 2 $, $ w(C) = 0 $.  \nExtend to strings: $ w(U) = \\sum_{x \\in U} w(x) \\mod 3 $.  \nThe transitions preserve $ w $ modulo 3.  \nAlso, the length modulo 2 is preserved: since each transition replaces 1 char with 2 or vice versa, $ |U| \\mod 2 $ is invariant under the reverse operations.\n\nThus, $ S[a_i..b_i] \\to T[c_i..d_i] $ is possible **iff**:  \n- $ w(S[a_i..b_i]) \\equiv w(T[c_i..d_i]) \\pmod{3} $, and  \n- $ |S[a_i..b_i]| \\equiv |T[c_i..d_i}| \\pmod{2} $","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF923D","tags":["constructive algorithms","implementation","strings"],"sample_group":[["AABCCBAAB\nABCB\n5\n1 3 1 2\n2 2 2 4\n7 9 1 1\n3 4 2 3\n4 5 1 3","10011"]],"created_at":"2026-03-03 11:00:39"}}