Odd Country

Description

在一片美丽的大陆上有 $100000$ 个国家,记为 $1$ 到 $100000$。这里经济发达,有数不尽的账房,并且每个国家有一个银行。某大公司的领袖在这 $100000$ 个银行开户时都存了 $3$ 大洋,他惜财如命,因此会不时地派小弟 GFS 清点一些银行的存款或者让 GFS 改变某个银行的存款。该村子在财产上的求和运算等同于我们的乘法运算,也就是说领袖开户时的存款总和为 $3^{100000}$。这里发行的软妹面额是最小的 $60$ 个素数 $(p_1 = 2, p_2 = 3, \dots, p_{60} = 281)$,任何人的财产都只能由这 $60$ 个基本面额表示,即设某个人的财产为 $\text{fortune}$(正整数),则 $\text{fortune} = p_1^{k_1} \cdot p_2^{k_2} \cdot \dots \cdot p_{60}^{k_{60}}$。

领袖习惯将一段编号连续的银行里的存款拿到一个账房去清点,为了避免 GFS 串通账房叛变,所以他不会每次都选择同一个账房。GFS 跟随领袖多年已经摸清了门路,知道领袖选择账房的方式。如果领袖选择清点编号在 $[a, b]$ 内的银行财产,他会先对 $[a, b]$ 的财产求和(计为 $\text{product}$),然后在编号属于 $[1, \text{product}]$ 的账房中选择一个去清点存款,检验自己计算是否正确同时也检验账房与 GFS 是否有勾结。GFS 发现如果某个账房的编号 $\text{number}$ 与 $\text{product}$ 相冲,领袖绝对不会选择这个账房。怎样才算与 $\text{product}$ 不相冲呢?若存在整数 $x,y$ 使得 $\text{number} \cdot x + \text{product} \cdot y = 1$,那么我们称 $\text{number}$ 与 $\text{product}$ 不相冲,即该账房有可能被领袖相中。当领袖又赚大钱了的时候,他会在某个银行改变存款,这样一来相同区间的银行在不同的时候算出来的 $\text{product}$ 可能是不一样的,而且领袖不会在某个银行的存款总数超过 $1000000$。

现在 GFS 预先知道了领袖的清点存款与变动存款的计划,想请你告诉他,每次清点存款时领袖有多少个账房可以供他选择,当然这个值可能非常大,GFS 只想知道对 $19961993$ 取模后的答案。

Input Format

第一行一个整数 $x$ 表示领袖清点和变动存款的总次数。

接下来 $x$ 行,每行 $3$ 个整数 $a_i, b_i, c_i$。$a_i$ 为 $0$ 时表示该条记录是清点计划,领袖会清点 $b_i$ 到 $c_i$ 的银行存款,你需要对该条记录计算出 GFS 想要的答案。$a_i$ 为 $1$ 时表示该条记录是存款变动,你要把银行 $b_i$ 的存款改为 $c_i$,不需要对该记录进行计算。

Output Format

输出若干行,每行一个数,表示那些年的答案。

Sample Input

6
0 1 3
1 1 5
0 1 3
1 1 7
0 1 3
0 2 3

Sample Output

18
24
36
6

Hint

$20\%$ 的数据满足: $x \leq 10000$,当 $a_i = 0$ 时 $0 \leq c_i − b_i \leq 100$; 每个 $\text{product} \leq 10^{18}$;

$30\%$ 的数据满足:$x \leq 50000$,当 $a_i = 0$ 时 $0 \leq c_i − b_i \leq 10000$;

$50\%$ 的数据满足:$x \leq 100000$,当 $a_i = 0$ 时 $0 \leq c_i − b_i \leq 100000$;

以上数据不重合,$20\% +30\% + 50\% = 100\%$,且保证满足题干要求。

 

分析:
  线段树维护乘积和包含哪些质数(状态压缩),然后欧拉函数做。

 

代码:

  1 #include <cstdio>
  2 #include <cstring>
  3 
  4 #define MOD 19961993ll
  5 #define MID (left + right >> 1)
  6 
  7 int f[300], p[100], pn;
  8 int n, q1, q2, q3;
  9 
 10 long long prod[400000], has[400000], rev[100], erci[100];
 11 
 12 long long tmp, tmphas;
 13 
 14 long long pow(long long num, long long mi)
 15 {
 16     if (mi == 0) return 1;
 17     if (mi == 1) return num;
 18     if (mi == 2) return num * num % MOD;
 19     return pow(pow(num, mi / 2), 2) * pow(num, mi % 2) % MOD;
 20 }
 21 
 22 void build(int i, int left, int right)
 23 {
 24     has[i] = 2;
 25     if (left != right)
 26     {
 27         build(i << 1, left, MID);
 28         build(i << 1 | 1, MID + 1, right);
 29         prod[i] = prod[i << 1] * prod[i << 1 | 1] % MOD;
 30         return;
 31     }
 32     prod[i] = 3;
 33 }
 34 
 35 void modify(int i, int left, int right, int pos, int data)
 36 {
 37     if (left == right)
 38     {
 39         prod[i] = data % MOD;
 40         has[i] = 0;
 41         for (int j = 0; j < 60; j++)
 42         {
 43             if (data % p[j] == 0)
 44             {
 45                 has[i] |= erci[j];
 46             }
 47         }
 48         return;
 49     }
 50     if (pos <= MID)
 51     {
 52         modify(i << 1, left, MID, pos, data);
 53     }
 54     else
 55     {
 56         modify(i << 1 | 1, MID + 1, right, pos, data);
 57     }
 58     prod[i] = prod[i << 1] * prod[i << 1 | 1] % MOD;
 59     has[i] = has[i << 1] | has[i << 1 | 1];
 60 }
 61 
 62 void query(int i, int left, int right, int ql, int qr)
 63 {
 64     if (ql <= left && qr >= right)
 65     {
 66         tmp = tmp * prod[i] % MOD;
 67         tmphas |= has[i];
 68         return;
 69     }
 70     if (qr < left || ql > right)
 71     {
 72         return;
 73     }
 74     if (ql <= MID)
 75     {
 76         query(i << 1, left, MID, ql, qr);
 77     }
 78     if (qr > MID)
 79     {
 80         query(i << 1 | 1, MID + 1, right, ql, qr);
 81     }
 82 }
 83 
 84 int main()
 85 {
 86     pn = 0;
 87     for (int i = 2; i < 300; i++)
 88     {
 89         if (!f[i])
 90         {
 91             rev[pn] = (i - 1) * pow(i, MOD - 2) % MOD;
 92             erci[pn] = (pn == 0 ? 1 : erci[pn - 1] << 1);
 93             p[pn] = i;
 94             pn++;
 95             for (int j = i + i; j < 300; j += i)
 96             {
 97                 f[j] = i;
 98             }
 99         }
100     }
101     build(1, 1, 100000);
102     scanf("%d", &n);
103     for (int i = 0; i < n; i++)
104     {
105         scanf("%d%d%d", &q1, &q2, &q3);
106         if (q1 == 0)
107         {
108             tmp = 1;
109             tmphas = 0;
110             query(1, 1, 100000, q2, q3);
111             for (int j = 0; j < 60; j++)
112             {
113                 if (tmphas & erci[j])
114                 {
115                     tmp = tmp * rev[j] % MOD;
116                 }
117             }
118             printf("%lld\n", tmp);
119         }
120         else
121         {
122             modify(1, 1, 100000, q2, q3);
123         }
124     }
125 }

 

转载于:https://www.cnblogs.com/lightning34/p/4591761.html

# CF1479D Odd Mineral Resource ## 题目描述 In Homer's country, there are $ n $ cities numbered $ 1 $ to $ n $ and they form a tree. That is, there are $ (n-1) $ undirected roads between these $ n $ cities and every two cities can reach each other through these roads. Homer's country is an industrial country, and each of the $ n $ cities in it contains some mineral resource. The mineral resource of city $ i $ is labeled $ a_i $ . Homer is given the plans of the country in the following $ q $ years. The plan of the $ i $ -th year is described by four parameters $ u_i, v_i, l_i $ and $ r_i $ , and he is asked to find any mineral resource $ c_i $ such that the following two conditions hold: - mineral resource $ c_i $ appears an odd number of times between city $ u_i $ and city $ v_i $ ; and - $ l_i \leq c_i \leq r_i $ . As the best friend of Homer, he asks you for help. For every plan, find any such mineral resource $ c_i $ , or tell him that there doesn't exist one. ## 输入格式 The first line contains two integers $ n $ ( $ 1 \leq n \leq 3 \cdot 10^5 $ ) and $ q $ ( $ 1 \leq q \leq 3 \cdot 10^5 $ ), indicating the number of cities and the number of plans. The second line contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ 1 \leq a_i \leq n $ ). Then the $ i $ -th line of the following $ (n-1) $ lines contains two integers $ x_i $ and $ y_i $ ( $ 1 \leq x_i, y_i \leq n $ ) with $ x_i \neq y_i $ , indicating that there is a bidirectional road between city $ x_i $ and city $ y_i $ . It is guaranteed that the given roads form a tree. Then the $ i $ -th line of the following $ q $ lines contains four integers $ u_i $ , $ v_i $ , $ l_i $ , $ r_i $ ( $ 1 \leq u_i \leq n $ , $ 1 \leq v_i \leq n $ , $ 1 \leq l_i \leq r_i \leq n $ ), indicating the plan of the $ i $ -th year. ## 输出格式 Print $ q $ lines, the $ i $ -th of which contains an integer $ c_i $ such that - $ c_i = {-1} $ if there is no such mineral resource that meets the required condition; or - $ c_i $ is the label of the chosen mineral resource of the $ i $ -th year. The chosen mineral resource $ c_i $ should meet those conditions in the $ i $ -th year described above in the problem statement. If there are multiple choices of $ c_i $ , you can print any of them. ## 输入输出样例 #1 ### 输入 #1 ``` 6 8 3 2 1 3 1 3 1 2 1 3 2 4 2 5 4 6 3 5 1 1 3 5 1 3 3 5 1 3 1 1 2 2 1 1 3 3 1 4 1 5 1 6 1 3 1 6 1 3 ``` ### 输出 #1 ``` -1 2 3 -1 3 2 2 3 ``` ## 说明/提示 In the first three queries, there are four cities between city $ 3 $ and city $ 5 $ , which are city $ 1 $ , city $ 2 $ , city $ 3 $ and city $ 5 $ . The mineral resources appear in them are mineral resources $ 1 $ (appears in city $ 3 $ and city $ 5 $ ), $ 2 $ (appears in city $ 2 $ ) and $ 3 $ (appears in city $ 1 $ ). It is noted that - The first query is only to check whether mineral source $ 1 $ appears an odd number of times between city $ 3 $ and city $ 5 $ . The answer is no, because mineral source $ 1 $ appears twice (an even number of times) between city $ 3 $ and city $ 5 $ . - The second and the third queries are the same but they can choose different mineral resources. Both mineral resources $ 2 $ and $ 3 $ are available. 为什么WA了 ```cpp #include <bits/stdc++.h> using namespace std; const int BITS = 20; const int MAX_N = 4e5 + 50; const int BSIZE = 1; int n, m, tot, a[MAX_N]; int fa[MAX_N][BITS], dep[MAX_N]; int dfn[MAX_N], in[MAX_N], out[MAX_N]; vector<int> e[MAX_N]; int L[MAX_N], R[MAX_N], pos[MAX_N]; int cnt[MAX_N], sum[MAX_N], ans[MAX_N]; struct Query { int l, r, p, L, R, id; bool operator < (const Query& rhs) const { if (pos[l] != pos[rhs.l]) return pos[l] < pos[rhs.l]; return (pos[l] & 1) ? r < rhs.r : r > rhs.r; } } q[MAX_N]; void init() { int cnt = n / BSIZE; for (int i = 1; i <= cnt; i++) { L[i] = (i - 1) * BSIZE + 1; R[i] = i * BSIZE; } if (R[cnt] < n) { L[cnt + 1] = R[cnt] + 1; R[++cnt] = n; } for (int i = 1; i <= cnt; i++) fill(pos + L[i], pos + R[i] + 1, i); } void modify(int idx) { cout << "modify " << idx << '\n'; sum[pos[idx]] -= cnt[idx]; cnt[idx] ^= 1; sum[pos[idx]] += cnt[idx]; } int query(int lt, int rt) { int lp = pos[lt], rp = pos[rt]; if (lp == rp) { for (int i = lt; i <= rt; i++) if (cnt[i]) return i; } else { for (int i = lt; i <= R[lp]; i++) if (cnt[i]) return i; for (int i = lp + 1; i < rp; i++) if (sum[i]) for (int j = L[i]; j <= R[i]; j++) if (cnt[j]) return j; for (int i = L[rp]; i <= rt; i++) if (cnt[i]) return i; } return -1; } void dfs(int u, int father, int depth) { fa[u][0] = father, dep[u] = depth; for (int k = 1; k < BITS; k++) fa[u][k] = fa[fa[u][k - 1]][k - 1]; in[u] = ++tot, dfn[tot] = u; for (int v : e[u]) if (v != father) dfs(v, u, depth + 1); out[u] = ++tot, dfn[tot] = u; } int LCA(int x, int y) { if (dep[x] > dep[y]) swap(x, y); for (int k = BITS - 1; ~k; k--) if (dep[x] <= dep[fa[y][k]]) y = fa[y][k]; if (x == y) return x; for (int k = BITS - 1; ~k; k--) if (fa[x][k] != fa[y][k]) x = fa[x][k], y = fa[y][k]; return x; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1, u, v; i < n; i++) { cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } dfs(1, 0, 1); for (int i = 1, u, v; i <= m; i++) { cin >> u >> v >> q[i].L >> q[i].R; q[i].id = i; if (in[u] > in[v]) swap(u, v); q[i].r = in[v]; int lca = LCA(u, v); if (lca == u) q[i].l = in[u], q[i].p = 0; else q[i].l = out[u], q[i].p = lca; } init(); sort(q + 1, q + m + 1); int l = 1, r = 0; for (int i = 1; i <= m; i++) { int ql = q[i].l, qr = q[i].r; while (l > ql) modify(a[dfn[--l]]); while (r < qr) modify(a[dfn[++r]]); while (l < ql) modify(a[dfn[l++]]); while (r > qr) modify(a[dfn[r--]]); if (q[i].p) modify(a[q[i].p]); cout << q[i].id << '\n'; ans[q[i].id] = query(q[i].L, q[i].R); if (q[i].p) modify(a[q[i].p]); } for (int i = 1; i <= n; i++) cout << ans[i] << '\n'; return 0; } ```
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值