每一列单调不减,则暴力维护每一列小矩形的高度表示从第
此时就将问题转化成了放置在同一水平线上的
枚举每一个柱子的高度,分别向左向右找到第一个比其小的柱子,那么这个高度的柱子所能围成的矩形的长即可决定,维护过程使用单调栈双向扫一遍就可以了。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
int a[2001][2001], h[2001][2001], l[2001], r[2001];
int count(int i)
{
<int> s;
stackint ans = 0;
for (int j = 1; j <= m; j++)
{
while (!s.empty() && h[i][j] < h[i][s.top()])
[s.top()] = j, s.pop();
r.push(j);
s}
while (!s.empty())
[s.top()] = m + 1, s.pop();
rfor (int j = m; j >= 1; j--)
{
while (!s.empty() && h[i][j] < h[i][s.top()])
[s.top()] = j, s.pop();
l.push(j);
s}
while (!s.empty())
[s.top()] = 0, s.pop();
lfor (int j = 1; j <= m; j++)
{
= max(ans, (r[j] - l[j] - 1) * h[i][j]);
ans }
return ans;
}
int main()
{
;
IOSint t;
>> t;
cin while (t--)
{
>> n >> m;
cin for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
>> a[i][j];
cin if (a[i][j] >= a[i - 1][j])
[i][j] = h[i - 1][j] + 1;
helse
[i][j] = 1;
h}
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
= max(ans, count(i));
ans }
<< ans << endl;
cout }
}
一张图为
给定无向无环图
分割数是给定的,类似于时间安排问题,思考二分,交了一发
观察得知如果两个原本属于不同组的结点如果可以划分到一组之中时,组数会减少
使用边结构体存图,对边权排序,每一次合并都对当前组数减
现在如果已经分成了break
。如果边权相等,则说明两者必须在一组,并查集合并。
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
const int M = 5e5 + 10;
int fa[N];
int n, m, k, t;
struct edge
{
int u, v, w;
bool operator<(const edge &p) const
{
return w < p.w;
}
} a[M];
int find(int x)
{
if (fa[x] != x)
{
[x] = find(fa[x]);
fa}
return fa[x];
}
void solve()
{
>> n >> m >> k;
cin for (int i = 1; i <= m; i++)
>> a[i].u >> a[i].v >> a[i].w;
cin for (int i = 1; i <= n; i++)
[i] = i;
fa(a + 1, a + m + 1);
sortint sum = n, d = 0;
for (int i = 1; i <= m; i++)
{
if (sum < k)
break;
if (sum == k && a[i].w != a[i - 1].w)
break;
if (find(a[i].u) == find(a[i].v))
continue;
[find(a[i].u)] = fa[find(a[i].v)];
fa= a[i].w;
d --;
sum}
if (sum == k)
<< d << endl;
cout else
<< -1 << endl;
cout }
signed main()
{
::sync_with_stdio(0);
ios.tie(0);
cin.tie(0);
cout>> t;
cin while (t--)
{
();
solve}
}
有
首先,偶数和
对于剩下的奇数,如果
再剩下的节点就没有办法,只能和
具体实现的过程就会发现,实际上每个数都去找了它最小的质因数进行连接,所有的比
离线线性筛打表即可。大范围预处理然后在线查询也可。
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
int n, t, sum, x;
int pri[N], f;
bool vis[N];
<int> q;
vectorsigned main()
{
::sync_with_stdio(0);
ios.tie(0);
cin.tie(0);
cout>> t;
cin for (int i = 1; i <= t; i++)
{
>> x;
cin .push_back(x);
q= max(x, n);
n }
for (int i = 2; i <= n; i++)
{
if (!vis[i])
[i] = 1, pri[++f] = i;
visfor (int j = 1; j <= f && i * pri[j] <= n; j++)
{
[i * pri[j]] = 1;
visif (i % pri[j] == 0)
break;
}
} // pri存了1-n的质数
for (auto i : q)
{
if (i == 2)
{
<< 0 << endl;
cout continue;
}
= ((3 + i) * (i - 2)) / 2;
sum // cout<<sum<<endl;
for (int j = 2; j <= f; j++)
{
if (pri[j] <= i)
+= pri[j];
sum else
break;
}
<< sum << endl;
cout }
}
给定一个长度为
区间异或和,不等式,
将第
如果
如果选择向相同方向走,则还需要继续检查。
如果第
如果始终查不到符合要求的,输出
时间容量吃紧,复杂度
还有一个细节,在建树的时候清理(初始化)
// #pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
using p = pair<int, int>;
#define IOS \
ios::sync_with_stdio(false); \
std::cin.tie(nullptr); \
std::cout.tie(nullptr);
#define int long long
const int INF = 1e18;
const int mod = 998244353;
const int N = 3e6 + 9;
int Trie[N][2];
int rem[N];
int tot = 1;
void insert(int x, int r)
{
int u = 1;
for (int i = 29; i >= 0; i--)
{
int flag = (x >> i) & 1LL;
if (!Trie[u][flag])
{
[u][flag] = ++tot;
Trie[tot][0] = Trie[tot][1] = 0;
Trie[tot] = 0;
rem}
= Trie[u][flag];
u [u] = max(rem[u], r);
rem}
}
int check(int pos, int tar, int k)
{
int u = 1;
int ans = 0;
for (int i = 29; i >= 0; i--)
{
int bittar = (tar >> i) & 1LL;
int bitk = (k >> i) & 1LL;
if (bitk)
{
= Trie[u][bittar ^ 1];
u }
else
{
if (Trie[u][bittar ^ 1])
{
= max(ans, rem[Trie[u][bittar ^ 1]]);
ans }
= Trie[u][bittar];
u }
if (!u)
return ans;
}
= max(ans, rem[u]);
ans return ans;
}
void solve()
{
= 1;
tot int n, k;
>> n >> k;
cin [1][1] = Trie[1][0] = 0;
Trie<int> a(n + 1);
vectorfor (int i = 1; i <= n; i++)
{
>> a[i];
cin if (i)
[i] ^= a[i - 1];
a}
int ans = 0;
(a[1], 1);
insert<int, int> ansf = {INF, INF};
pairint len = INF;
for (int i = 2; i <= n; i++)
{
= check(i - 1, a[i], k);
ans if (ans > 0)
{
if (i - ans < len)
{
= i - ans;
len = {ans + 1, i};
ansf }
else if (i - ans == len)
{
= min(ansf, {ans + 1, i});
ansf }
}
(a[i], i);
insert}
if (len == INF)
{
<< -1 << endl;
cout return;
}
<< ansf.first << " " << ansf.second << endl;
cout }
signed main()
{
;
IOSint t;
>> t;
cin while (t--)
{
();
solve}
("pause");
systemreturn 0;
}
现在你有
如果球在传球途中不能回到
这个方程不可能没有解(因为其可构成一个完全剩余系)
所以证明球中途可以回到初始人的手中。
考虑第
故有递推方程
问题回到求解指数方程
// #pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
#define int long long
const int INF = 1e18;
const int mod = 998244353;
const int N = 36007; // N 是最大可以存储的元素数量
class Hash
{
private:
int keys[N];
int values[N];
public:
() { memset(values, 0, sizeof(values)); }
Hash
int &operator[](int n)
{
int idx = (n % N + N) % N, cnt = 1;
while (keys[idx] != n && values[idx] != 0)
{
= (idx + cnt * cnt) % N;
idx += 1;
cnt }
[idx] = n;
keysreturn values[idx];
}
};
int qpow(int a, int b)
{ // 快速幂
int ans = 1;
while (b)
{
if (b & 1)
= (ans * a) % mod;
ans = (a * a) % mod;
a >>= 1;
b }
return ans;
}
int bsgs(int a, int b)
{
;
Hash mpif (a % mod == b % mod)
return 1;
if (a % mod == 0 && b)
return -1;
int unit = (int)ceil(sqrt(mod)), tmp = qpow(a, unit);
for (int i = 0; i <= unit; ++i)
[b] = i, b = (b * a) % mod;
mp= 1;
b for (int i = 1; i <= unit; ++i)
{
= (b * tmp) % mod;
b if (mp[b])
return i * unit - mp[b];
}
return -1;
}
void solve()
{
int n, x;
>> n >> x;
cin if (x == 1)
{
<< 0 << endl;
cout return;
}
if (x == 0)
{
<< 1 << endl;
cout return;
}
int a = n - 1;
int tt = (n * x + (n - 1));
int k1 = bsgs(a, tt % mod);
if (k1 % 2 == 0)
= -1;
k1 = (n * x - (n - 1)) + mod;
tt int k2 = bsgs(a, tt % mod);
if (k2 & 1)
= -1;
k2 if (k1 != -1 && k2 != -1)
<< min(k1, k2) << endl;
cout else if (k1 != -1 && k2 == -1)
<< k1 << endl;
cout else if (k2 != -1 && k1 == -1)
<< k2 << endl;
cout else
<< -1 << endl;
cout return;
}
signed main()
{
;
IOSint t;
>> t;
cin while (t--)
{
();
solve}
("pause");
systemreturn 0;
}
给定一个
Sample Input
5
ABCBC ACAAB
Sample output
Yes..B
AC.BA.C
.BA.
C.C.
BA..CBA
数据很小,暴力搜索,问题是对谁进行深度搜索,状态又如何表示。
注意到
对行进行搜索,保证搜索时能够拼出来合法的侧视图,这需要预处理出所有可能的排列(注意排列需要长度为next_permutation
函数即可。
我们只需要核实每一列是否都只有一个
每一列只有四个判定指标:
故使用一个
如果最后数字归零,证明搜索成立,结果合法。
#include <bits/stdc++.h>
using namespace std;
int n;
, c;
string r<string> graph;
vector<char, set<string>> mp;
mapbool flag = 0;
/* 1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1 高三位表示该列是否检查过,低1位表示对应字母是否占用
*/
int check;
void dfs(int i, int check)
{
if (flag)
return;
if (i == n)
{
if (check == 0)
{
<< "Yes" << endl;
cout for (auto &nx : graph)
{
<< nx << endl;
cout }
= 1;
flag }
return;
}
for (auto str : mp[r[i]])
{
.push_back(str);
graphint nowcheck = check;
bool accepted = 1;
for (int j = 0; j < n; j++)
{
if (str[j] == '.')
continue;
int pos = 4 * j + (str[j] - 'A');
if ((nowcheck & (1 << pos)) == 0)
{
= 0;
accepted break;
}
^= (1 << pos);
nowcheck if (nowcheck & (1 << (4 * j + 3)))
{
if (str[j] != c[j])
{
= 0;
accepted break;
}
^= (1 << (4 * j + 3));
nowcheck }
}
if (accepted)
(i + 1, nowcheck);
dfs.pop_back();
graph}
}
int main()
{
>> n >> r >> c;
cin <int> abc(n);
vector(abc.begin(), abc.end(), 0);
iota// 0-2ABC,3.
do
{
;
string schar c = 0;
for (auto p : abc)
{
if (p >= 0 && p <= 2)
{
+= char('A' + p);
s if (c == 0)
= 'A' + p;
c }
else
+= '.';
s }
[c].insert(s);
mp
} while (next_permutation(abc.begin(), abc.end()));
= (1 << (4 * n)) - 1;
check (0, check);
dfsif (!flag)
<< "No" << endl;
cout ("pause");
systemreturn 0;
}
有一个没有自环和重边的图,给定一个最短路径数组,要求复原出一个满足条件的图,且保证每个顶点的度不超过
暴力即可,这里主要谈一下为什么不考虑环的问题,显然,如果存在一个
#include <bits/stdc++.h>
using namespace std;
using p = pair<int, int>;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
#define int long long
const int INF = 1e18;
const int mod = 998244353;
void solve()
{
int n, k;
>> n >> k;
cin <int> a(n);
vector<int, int> dist;
multimapfor (int i = 0; i < n; i++)
{
>> a[i];
cin .insert({a[i], i});
dist}
<int> degree(n);
vectorint posnow;
bool flag = 0;
<pair<int, int>> rem;
vectorint remch = -1;
<int> noww, nextt;
queuefor (auto [x, y] : dist) // x距离 y节点号
{
bool tag = 0;
if (x - remch > 1)
{
<< "-1" << endl;
cout return;
}
if (x - remch == 1)
= 1;
tag = x;
remch if (!x)
{
if (!flag)
{
= 1, posnow = y;
flag .push(y);
nexttcontinue;
}
else
{
<< "-1" << endl;
cout return;
}
}
if (tag)
{
= nextt;
noww = queue<int>();
nextt if (noww.empty())
{
<< "-1" << endl;
cout return;
}
= noww.front();
posnow .pop();
noww}
if (degree[posnow] >= k)
{
if (noww.empty())
{
<< "-1" << endl;
cout return;
}
= noww.front();
posnow .pop();
noww}
[posnow]++;
degree[y]++;
degree.push_back({posnow, y});
rem.push(y);
nextt}
<< rem.size() << endl;
cout for (auto [x, y] : rem)
{
<< x + 1 << " " << y + 1 << endl;
cout }
return;
}
signed main()
{
;
IOSint t;
= 1;
t while (t--)
{
();
solve}
("pause");
systemreturn 0;
}
给我们一棵有根的树,它由
每个顶点上都写有一个数字,最初所有数字都等于
对于每个
在一次操作中,我们可以完成以下操作:
实现我们的目标至少需要多少次运算?
正着想很难想,考虑倒着从叶子结点开始考虑。
贪心的想,每一个叶子结点都是流满最大流量的,此时可以使得其前序序列所能经过的流量
对于叶子结点的父节点,如果其叶子结点的流量和大于等于其的上界
如果叶子结点的流量和在其
如果叶子结点流量和不够,则说明其必须需要补充流量,那每一次补充都要利用到极致,所以直接把该父节点补满,总和记作
#include <bits/stdc++.h>
using namespace std;
using p = pair<int, int>;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
#define int long long
const int INF = 1e18;
const int mod = 998244353;
const int N = 1e5 + 5;
<vector<int>> connects;
vector<bool> vis;
vector<pair<int, int>> information;
vectorvoid add_edge(int u, int v)
{
[u].push_back(v);
connects[v].push_back(u);
connects}
int ans = 0;
void init(int n)
{
= 0;
ans = vector<vector<int>>(n + 1);
connects = vector<bool>(n + 1);
vis = vector<pair<int, int>>(n + 1);
information for (int i = 0; i <= n; i++)
{
[i] = false;
vis[i] = {0, 0};
information}
}
int dfs(int pos)
{
[pos] = true;
visint res = 0;
for (auto i : connects[pos])
{
if (!vis[i])
{
+= dfs(i);
res [i] = 0;
vis}
}
if (!res) // 叶子结点
{
++;
ansreturn information[pos].second;
}
if (res < information[pos].first)
{
++;
ansreturn information[pos].second;
}
if (res > information[pos].second)
{
return information[pos].second;
}
return res;
}
void solve()
{
int n;
>> n;
cin (n);
initfor (int i = 2; i <= n; i++)
{
int u;
>> u;
cin (i, u);
add_edge}
for (int i = 1; i <= n; i++)
{
>> information[i].first >> information[i].second;
cin }
(1);
dfs<< ans << endl;
cout }
signed main()
{
;
IOSint t;
>> t;
cin while (t--)
{
();
solve}
// system("pause");
return 0;
}
AmShZ 已从伊朗前往意大利参加 Thom Yorke 演唱会。意大利有
每天一开始,AmShZ 可以向 Keshi 发送以下两条信息中的一条:
AmShZ 向 Keshi 发送一条道路的索引,将其作为被封锁的道路。然后 Keshi 就会明白,他永远都不应该使用这条路,他将在当前城市停留一天。
AmShZ 告诉凯希移动。然后,凯西会随机选择一个可以从当前城市到达的城市,并移动到那里。(如果从城市
请注意,AmShZ 总是知道 Keshi 的当前位置。
AmShZ 和 Keshi 想要找到一个最小的整数
设来访者此时站在
一个朴素的想法就是断掉除指定路径外所有的边,将其作为边权的一部分跑最短路,但是这样有一个问题,其需要找到所有的符合条件的最短路并且保存其路径,寻找路径的交点来调整断边(因为可能多断了没必要的边),这样很麻烦。
换一种思路来想,如果来访者站在
如果有相等边权的边的话,因为取
所以我们从小到大拿
这就是队列优化
因为
所以对题目进行反向建图,通过
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
<vector<int>> connects;
vector<int> dist;
vector<int> degrees;
vector<int> visited;
vectorvoid init(int n)
{
++;
n.resize(n, vector<int>());
connects.resize(n, INF);
dist.resize(n, 0);
degrees.resize(n, 0);
visited}
void add_edge(int u, int v)
{
[u].push_back(v);
connects[v]++;
degrees}
int Dijkstra_queue(int st, int ed) // 反向Dijkstra
{
<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
priority_queue.push({0, ed});
q[ed] = 0;
distwhile (!q.empty())
{
auto [d, u] = q.top();
.pop();
qif (visited[u])
continue;
[u] = true;
visitedfor (auto v : connects[u])
{
if (dist[v] > dist[u] + degrees[v])
{
[v] = dist[u] + degrees[v];
dist.push({dist[v], v});
q}
[v]--;
degrees}
}
return dist[st];
}
void solve()
{
int n, m;
>> n >> m;
cin (n);
initfor (int i = 0; i < m; i++)
{
int u, v;
>> u >> v;
cin (v, u);
add_edge}
<< Dijkstra_queue(1, n) << endl;
cout return;
}
signed main()
{
;
IOSint t;
= 1;
t while (t--)
();
solvereturn 0;
}
一共有
类似于上题,从某个怪兽
但是有个问题在于,这个
如果一个点其儿子的所有最小伤害都确定且和物理伤害加起来都没有法术斩杀伤害高,那显然选择使用后者。
此时,怪兽
也就是说,一个确定的结点的充分必要条件,是其确定值为法术伤害
所以对于每一个节点
这就又是
问题是,初始状态是谁?这里显然没有明确的起点,也不可以建立一个大型超级终点,因为入堆的方式严格要求入堆点的入度为
哦我们考虑一个问题,因为每个怪兽被物理攻击后必然会分裂,没有物理攻击就能直接消灭的怪兽,所以一定有至少一个怪兽必须使用法术伤害消灭。那么这些必须使用法术伤害消灭的一定是比较小的(法术伤害最小的那个怪兽一定是直接法术斩杀,因为其不可能放弃最小化的方式去使用物理伤害兜圈)
所以法术伤害最小的那个节点状态一定是确定的,其必须作为起点状态入堆。
问题是,只有这一个起点对吗?
显然不对,因为只用这一个法术伤害斩杀目标,此时必然存在一种可能,其更新不动了,没有新节点入堆,队列变空,和超级终点的问题一样了。
所以,需要将所有节点的法术伤害全部入队列,保证更新不动的时候会选择更多的怪兽直接使用法术伤害斩杀。而且由于
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
const int maxn = 2e5 + 10;
<int> connects[maxn];
vectorint s[maxn], k[maxn], degree[maxn], dp[maxn];
int n;
bool vis[maxn];
void add_edge(int u, int v)
{
[u].push_back(v);
connects}
typedef struct node
{
int pos;
int dis;
bool operator<(const node &x) const
{
return x.dis < dis;
}
} node;
void Dikstra_queue()
{
<node> q;
priority_queuefor (int i = 1; i <= n; i++)
{
.push({i, dp[i]}); // 初始化,预先把所有的法术伤害作为杀死方式预选择丢进去
q// 如果某个怪物的法术斩杀伤害是最小的,那么这个怪兽必然是使用法术斩杀的,其不可能再去使用物理伤害兜圈
// 为什么全丢进去,是当某个节点的物理伤害计算发现其比法术斩杀还要大,那这个点就是用法术斩杀打死的
// 如果最小的法术伤害绕着走了一圈之后没有发生新入队行为,那么就更新不动了,队列变空,
//则说明如果某些可能度数没有归零的点的怪兽想使用物理伤害杀死,必须使用法术伤害干死的怪兽不止一个
//答案就错误了,所以必须把所有的都丢进去以初始化才可以
}
while (!q.empty())
{
auto [pos, dpp] = q.top();
.pop();
qif (vis[pos])
continue;
[pos] = 1;
vis[pos] = dpp; // 固定杀死方式
dpfor (auto v : connects[pos])
{
if (vis[v]) // 已经固定杀死方式,即dp值已确定
continue;
if (s[v] + dpp >= dp[v]) // 还没加完的物理攻击就已经比dp值大,此时不更新
continue;
[v] += dpp;
s[v]--;
degreeif (!degree[v])
{
.push({v, s[v]});
q}
}
}
<< dp[1] << endl;
cout }
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
signed main()
{
;
IOS>> n;
cin for (int i = 1; i <= n; i++)
{
>> s[i] >> k[i] >> degree[i];
cin [i] = k[i];
dpfor (int j = 1; j <= degree[i]; j++)
{
int x;
>> x;
cin (x, i);
add_edge}
}
();
Dikstra_queue("pause");
systemreturn 0;
}
这是一个交互问题。
你需要去猜一个长度为
? 1 i
,评测机返回第? 2 l r
,评测机返回区间第一个问题不允许超过
此次遍历最坏花费了
剩余
记两个新字符第一次出现的位置为
判断每一位的字符是谁的唯一方式(因为询问
暴力每一个
#include <bits/stdc++.h>
using namespace std;
char ask_one(int i)
{
<< "? 1 " << i << endl;
cout char c;
>> c;
cin (stdout);
fflush(stdin);
fflushreturn c;
}
int ask_line(int l, int r)
{
<< "? 2 " << l << " " << r << endl;
cout int x;
>> x;
cin (stdout);
fflush(stdin);
fflushreturn x;
}
int main()
{
int n;
>> n;
cin (stdout);
fflush(stdin);
fflush<int> remrangedif;
vector;
string s+= ask_one(1);
s .push_back(1);
remrangediffor (int i = 1; i < n; i++)
{
int size = ask_line(1, i + 1);
if (size > remrangedif[0])
{
+= ask_one(i + 1);
s }
else
{
<char, int> last;
mapfor (int j = 0; j < s.size(); j++)
[s[j]] = j; // 记录每个字母最后出现的位置
last<int> lasts;
vectorfor (auto x : last)
.push_back(x.second);
lasts(lasts.begin(), lasts.end()); // 最后出现的位置排序
sortint l = 0;
int r = lasts.size();
while (r - l > 1)
{
int m = (l + r) >> 1LL;
int c = ask_line(lasts[m] + 1, i + 1);
if (c == remrangedif[lasts[m]])
= m;
l else
= m;
r }
+= s[lasts[l]];
s .push_back({size});
remrangedif}
= vector<int>(i + 1);
remrangedif <char, int> q;
mapfor (int j = i; j >= 0; j--)
{
[s[j]]++;
q[j] = q.size();
remrangedif}
}
<< "! " << s << endl;
cout (stdout);
fflush(stdin);
fflushreturn 0;
}
给定
即如果
等价于
考虑朴素的枚举每一个
实际上我们可以转化一下模型。对于一个数
for(int j = 0; j < n; j++)
for(int i = 0; i < 1 << n; i++)
if(i >> j & 1) f[i] += f[i ^ (1 << j)];
如果要维护
for(int j = 0; j < n; j++)
for(int i = 0; i < 1 << n; i++)
if(!(i >> j & 1)) f[i] += f[i ^ (1 << j)];
给定两个长度为
则有
考虑
故可分开成两个集合
分别维护子集的最大和最小值即可,注意这里维护的是超集(给定
考虑原集合
故有
#include <bits/stdc++.h>
using namespace std;
using p = pair<int, int>;
#define IOS \
ios::sync_with_stdio(false); \
cin.tie(nullptr); \
cout.tie(nullptr);
#define int long long
const int INF = 1e18;
const int mod = 998244353;
void solve()
{
int n;
>> n;
cin int m = 1;
while (m<n)
<<=1LL;
m<int> a(n), b(n), c(m+1, -INF), maxa(m, -INF), maxb(m, -INF), mina(m, INF), minb(m, INF);
vectorfor (int i = 0; i < n; i++)
{
>> a[i];
cin [i] = mina[i] = a[i];
maxa}
for (int i = 0; i < n; i++)
{
>> b[i];
cin [i] = minb[i] = b[i];
maxb}
for(int i=n;i<m;i++)
{
[i]=maxb[i]=-INF;
maxa[i]=minb[i]=INF;
mina}
for(int j=0;(1LL<<j)<m;j++)
{
for(int i=0;i<=n-1;i++)
{
if(!(i>>j&1LL))
{
[i]=max(maxa[i],maxa[i^(1LL<<j)]);
maxa[i]=max(maxb[i],maxb[i^(1LL<<j)]);
maxb[i]=min(mina[i],mina[i^(1LL<<j)]);
mina[i]=min(minb[i],minb[i^(1LL<<j)]);
minb
}
}
}
int ans=0;
for (int i = n - 1; i >= 0; i--)
{
[i]=max(max(maxa[i]*maxb[i],mina[i]*minb[i]),max(maxa[i]*minb[i],maxb[i]*mina[i]));
c[i] = max(c[i], c[i + 1]);
c(ans += (mod + c[i])) %= mod;
}
<< (ans % mod + mod) % mod << endl;
cout }
signed main()
{
;
IOSint t;
>> t;
cin while (t--)
{
();
solve}
// system("pause");
return 0;
}
给定一棵
1 a b
查询2 a
查询当前树链剖分模板题,找到
注意,
注意,此题目如下写法会炸邻接表,需要使用链式前向星进行存边,而且还好写
// #pragma GCC optimize(2)
#pragma comment(linker, "/STACK:10240000000,10240000000")
#include <Bits/stdc++.h>
//#define DEBUG 1
using namespace std;
#define int long long
#define ll long long
const int maxn = 2e5 + 9;
struct BITtree
{
;
ll n<ll> Bit;
vector(ll n) : n(n), Bit(vector<ll>(n+1, 0)) {}
BITtree() { BITtree(0); }
BITtree(ll x) { return (x & (-x)); }
ll lowBitvoid update(ll pos, ll x)
{
for (; pos <= n; pos += lowBit(pos))
[pos] += x;
Bit}
(ll pos)
ll query{
= 0;
ll ans for (; pos; pos -= lowBit(pos))
+= Bit[pos];
ans return ans;
}
};
int father[maxn], sizes[maxn], hson[maxn], depth[maxn], top[maxn], ranks[maxn], dfn[maxn], cnt;
int nxt[maxn << 1LL], head[maxn], to[maxn << 1LL], tot;
void add(int u, int v)
{
[++tot] = head[u];
nxt[u] = tot;
head[tot] = v;
to}
void add_edge(int u, int v)
{
(u, v);
add(v, u);
add}
void dfs1(int pos)
{
[pos] = -1;
hson[pos] = 1;
sizes#ifdef DEBUG
<< endl;
cout << "-----------DEBUG-----------" << endl;
cout << "now pos is " << pos << endl;
cout << "now sons are ";
cout for (int i = head[pos]; i; i = nxt[i])
{
<< to[i] << " ";
cout }
<< endl;
cout << "-----------DEBUG-----------" << endl;
cout #endif
for (int i = head[pos]; i; i = nxt[i])
{
if (!depth[to[i]])
{
[to[i]] = depth[pos] + 1;
depth[to[i]] = pos;
father(to[i]);
dfs1[pos] += sizes[to[i]];
sizesif (hson[pos] == -1 || sizes[to[i]] > sizes[hson[pos]])
[pos] = to[i];
hson}
}
}
void dfs2(int u, int tops)
{
[u] = tops;
top[u] = ++cnt;
dfn[cnt] = u;
ranksif (hson[u] != -1)
{
(hson[u], tops);
dfs2for (int i = head[u]; i; i = nxt[i])
{
if (to[i] == father[u] || to[i] == hson[u])
continue;
(to[i], to[i]);
dfs2}
}
}
int lca(int u, int v)
{
while (top[u] != top[v])
{
if (depth[top[u]] < depth[top[v]])
(u, v);
swap= father[top[u]]; // 所在重链首低的向上跳
u }
return depth[u] < depth[v] ? u : v;
}
, tree2, tree3;
BITtree tree1signed main()
{
::sync_with_stdio(0);
ios.tie(0);
cin.tie(0);
cout//freopen("1002.in", "r", stdin);
//freopen("1002.myout", "w", stdout);
int n;
>> n;
cin for (int i = 1; i < n; i++)
{
int u, v;
>> u >> v;
cin (u, v);
add_edge}
[1] = 1;
depth(1);
dfs1(1, 1);
dfs2#ifdef DEBUG
for (int i = 1; i <= n; i++)
("%lld ", dfn[i]);
printf("\n");
printffor (int i = 1; i <= n; i++)
("%lld ", top[i]);
printf("\n");
printffor (int i = 1; i <= n; i++)
("%lld ", depth[i]);
printf("\n");
printf#endif
= BITtree(n);
tree1 = BITtree(n);
tree2 = BITtree(n);
tree3 int q;
>> q;
cin while (q--)
{
int op, u, v;
>> op >> u;
cin if (op == 1)
{
>> v;
cin int lcas = lca(u, v);
int s = u;
int l, r;
int k1 = -depth[u] - 1;
#ifdef DEBUG
("lca:%lld\n", lcas);
printf("case1\n");
printf("k1:%lld\n", k1);
printf#endif
while (top[s] != top[lcas]) // s向上跳链,(s,top[s])必然连续
{
= dfn[top[s]], r = dfn[s];
l #ifdef DEBUG
("l:%lld r:%lld\n", l, r);
printf("s:%lld top[s]:%lld\n", s, top[s]);
printf#endif
.update(l, 1);
tree1.update(r + 1, -1);
tree1.update(l, 2 * k1);
tree2.update(r + 1, -2 * k1);
tree2.update(l, k1 * k1);
tree3.update(r + 1, -k1 * k1);
tree3= father[top[s]];
s }
int q = v;
int k2 = depth[u] - 2 * depth[lcas] + 1;
#ifdef DEBUG
("case2\n");
printf("k2:%lld\n", k2);
printf#endif
while (top[q] != top[lcas])
{
= dfn[top[q]], r = dfn[q];
l #ifdef DEBUG
("l:%lld r:%lld\n", l, r);
printf("q:%lld top[q]:%lld\n", q, top[q]);
printf#endif
.update(l, 1);
tree1.update(r + 1, -1);
tree1.update(l, 2 * k2);
tree2.update(r + 1, -2 * k2);
tree2.update(l, k2 * k2);
tree3.update(r + 1, -k2 * k2);
tree3= father[top[q]];
q }
= dfn[s], r = dfn[q];
l #ifdef DEBUG
("l:%lld r:%lld\n", l, r);
printf("s:%lld q:%lld\n", s, q);
printf#endif
if (l <= r)
{
.update(l, 1);
tree1.update(r + 1, -1);
tree1.update(l, 2 * k2);
tree2.update(r + 1, -2 * k2);
tree2.update(l, k2 * k2);
tree3.update(r + 1, -k2 * k2);
tree3}
else
{
.update(r, 1);
tree1.update(l + 1, -1);
tree1.update(r, 2 * k1);
tree2.update(l + 1, -2 * k1);
tree2.update(r, k1 * k1);
tree3.update(l + 1, -k1 * k1);
tree3}
}
else
{
= 0;
ll ans #ifdef DEBUG
("ans\n");
printf("dfn:%lld\n", dfn[u]);
printf("tree1:%lld\n", tree1.query(dfn[u]));
printf("tree2:%lld\n", tree2.query(dfn[u]));
printf("tree3:%lld\n", tree3.query(dfn[u]));
printf#endif
+= 1LL * tree1.query(dfn[u]) * depth[u] * depth[u];
ans += 1LL * tree2.query(dfn[u]) * depth[u];
ans += 1LL * tree3.query(dfn[u]);
ans << ans << endl;
cout }
}
// system("pause");
return 0;
}
给定一个数
显然生成数列必然构成了一个
问题就转换成了计算排列的符号。
根据高等代数定义,排列的符号
则排列中有
而排列必然可以写成一系列对换的乘积,根据乘法交换性可以得知上述等式必然成立。
而
#include <bits/stdc++.h>
using namespace std;
#define int __int128_t
int quickpow(int a, int b, int p)
{
int res = 1;
while (b)
{
if (b & 1)
= res * a % p;
res = a * a % p;
a >>= 1;
b }
return res;
}
inline void read(int &x)
{
int f = 1;
= 0;
x char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
= -1;
f = getchar();
ch }
while (ch >= '0' && ch <= '9')
{
= x * 10 + ch - '0';
x = getchar();
ch }
*= f;
x }
inline void write(__int128_t x)
{
if (x < 0)
{
('-');
putchar= -x;
x }
if (x > 9)
(x / 10);
write(x % 10 + '0');
putchar}
void solve()
{
int a, p;
(a), read(p);
readint ans = quickpow(a, (p - 1) / 2, p);
if (ans == 1)
<< 0 << endl;
cout else
<< 1 << endl;
cout }
signed main()
{
int t;
(t);
readwhile (t--)
();
solvereturn 0;
}
定义
给定
数据包括多组测试样例,保证每组不多于
棘手的问题。
由于
暴力枚举
考虑两个的
考虑连续三个数字的情况,连续三个数字,同时能被
考虑连续四个数字的情况,显然,必然有两个被
以此类推,连续序列的
剩下的连续
假定要筛除区间(l+p-1)/p
.
然后处理各个数的欧拉函数,这个是个很技巧的问题,因为埃氏筛法不可以使用欧拉函数性实现递推。解决方法是使用欧拉函数的定义式。
暴力维护%
,直接使用unsigned int
自然溢出即可,否则会#define int long long
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int mod = (1LL << 32);
<int, int> phi;
map<int, int> smallphi;
mapconst int maxn = 1e6 + 10;
int prime[maxn], smallprime[maxn], cnt, smallcnt;
bool is_prime[maxn], is_smallprime[maxn];
void init(int l, int r)
{
[1] = 0;
is_smallprimefor (int i = 2; i * i <= r; i++)
{
if (!is_smallprime[i])
{
[++smallcnt] = i;
smallprime[i] = 1;
is_smallprime[i] = i - 1;
smallphi}
for (int j = 1; j <= smallcnt && i * smallprime[j] <= r; i++)
{
[i * smallprime[j]] = 0;
is_smallprime[i * smallprime[j]] = smallphi[i] * smallphi[smallprime[j]];
smallphiif (i % smallprime[j] == 0)
{
[i * smallprime[j]] = smallprime[j] * smallphi[i];
smallphibreak;
}
}
}
}
int interval_phi(int l, int r)
{
<int> vis(r - l + 1), phi(r - l + 1);
vector// 区间筛,vis[i]表示i+l的值,phi[i]表示i+l的欧拉函数值
// 初始化,通过vis判断目标是否为质数,模拟质因数分解过程;phi运用欧拉函数公式phi(n)=n*\prod_{p|n}(1-1/p)
for (int i = 0; i <= r - l; i++)
{
[i] = i + l;
vis[i] = i + l;
phi}
// 区间筛,枚举小质数,对区间内的数进行筛选,若vis[i]是smallprime[j]的倍数,则phi[i]=phi[i]/smallprime[j]*(smallprime[j]-1)
for (int j = 1; j <= smallcnt && smallprime[j] * smallprime[j] <= r; j++)
{
for (int i = max(2ll, (l + smallprime[j] - 1) / smallprime[j]); i * smallprime[j] <= r; i++)
{
if (vis[i * smallprime[j] - l] % smallprime[j] == 0)
{
[i * smallprime[j] - l] = phi[i * smallprime[j] - l] / smallprime[j] * (smallprime[j] - 1);
phiwhile (vis[i * smallprime[j] - l] % smallprime[j] == 0)
[i * smallprime[j] - l] /= smallprime[j];
vis}
}
}
for (int i = 0; i <= r - l; i++)
{
if (vis[i] > 1)
{
[i] = phi[i] / vis[i] * (vis[i] - 1);
phi}
}
int ans = 0;
for (int i = 0; i < r - l; i++)
{
(ans += (phi[i] * phi[i + 1])) %= mod;
}
return ans;
}
signed main()
{
int t = 1000;
("6biao.txt", "w", stdout);
freopen(1, 1e9);
init<int> ans(1001);
vectorfor (int i = 0; i < t; i++)
{
int l, r;
= 1e6 * (i);
l = 1e6 * (i + 1);
r [i + 1] = (interval_phi(l, r));
ans(ans[i + 1] += ans[i]) %= mod;
<< ans[i + 1] << endl;
cout (stdout);
fflush}
return 0;
}
由于调和级数原理,时间复杂度
然后暴力预处理
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const int maxn = 3e6 + 10;
int prime[maxn];
bool not_prime[maxn];
int phi[maxn];
int cnt = 0;
int Prime[maxn], Phi[maxn];
int Vis[maxn];
void PreEulerTable(int r)
{
[1] = 1, not_prime[1] = 0;
phifor (int i = 2; i <= r; i++)
{
int temp = cnt;
if (!not_prime[i])
{
[++cnt] = i;
prime[i] = i - 1;
phi}
for (int j = 1; j <= cnt && (i * prime[j] <= r); j++)
{
[i * prime[j]] = 1;
not_prime[i * prime[j]] = phi[i] * phi[prime[j]];
phiif (i % prime[j] == 0)
{
[i * prime[j]] = phi[i] * prime[j];
phibreak;
}
}
}
}
(int l, int r) //[l(l+1),r(r+1) )
ui query{
= 0;
ui ans for (int i = 0; i <= r - l; i++)
{
[i] = Phi[i] = i + l;
Vis}
for (int i = 1; i <= cnt && (prime[i] * prime[i] <= r); i++)
{
for (int j = max(2, (l + prime[i] - 1) / prime[i]) * prime[i]; j <= r; j += prime[i])
{
if (Vis[j - l] % prime[i] == 0)
{
[j - l] = Phi[j - l] / prime[i] * (prime[i] - 1);
Phiwhile (Vis[j - l] % prime[i] == 0)
[j - l] /= prime[i];
Vis}
}
}
for (int i = 0; i <= r - l; i++)
{
if (Vis[i] > 1)
[i] = Phi[i] / Vis[i] * (Vis[i] - 1);
Phi}
for (int i = 0; i < r - l; i++)
{
+= ((ll)Phi[i] * Phi[i + 1]);
ans }
return ans;
}
<pair<ll, ui>> bigans;
vectorvoid pre()
{
for (int i = 1; i < 2e6; i++)
{
= (ll)i * (i + 1), phi_ = (ll)phi[i] * (ll)phi[i + 1];
ll now if (__int128_t(now) * (i + 2) > 2e18)
break;
for (int j = i + 2; j < 2e6; j++)
{
= __gcd(now, ll(j));
ll gcd1 = j / gcd1;
ll tem if (__int128_t(now) * tem > 1e18)
break;
= __gcd(now, tem);
ll gcd2 *= tem;
now phi_ *= (phi[tem] * gcd2 / phi[gcd2]); // 精度炸了
= phi_;
ui rem .push_back({now, rem});
bigans}
}
(bigans.begin(), bigans.end());
sortint l = bigans.size();
// cout << l << endl;
for (int i = l - 2; i >= 0; i--)
{
[i].second += bigans[i + 1].second;
bigans}
return;
}
[1001] = {0, 2298992963, 20737083, 513597167, 1721646463, 2806397395, 2802122835, 1485983503, 3091413427, 4208310523, 2831606407, 1628710319, 2817038135, 928385067, 3863807535, 3676485623, 767875647, 2313059015, 1137679023, 396967215, 2304634307, 2890862199, 90066623, 558603471, 1679200987, 3911243011, 982845519, 1670422743, 4093650807, 2151241015, 863633819, 1914418175, 1180229931, 890457979, 2725678339, 1513164495, 716799111, 1262781239, 3686770103, 1121843051, 1078090151, 834258119, 418144215, 1591360999, 1780791579, 174116147, 3241891131, 1141318943, 997398119, 3250717543, 3114793999, 3706497599, 2037260823, 3040210079, 1305807835, 1723687815, 1135940691, 3314489459, 949356871, 3081495243, 594017127, 1622159175, 1224816023, 1859397967, 2147536439, 2490547939, 2745824391, 3569126119, 209337115, 1385386507, 3287781831, 1511510295, 284723135, 4085720447, 2725134651, 3013309179, 71441799, 1556277167, 3658898795, 1085308915, 1678514911, 3456120263, 546233879, 2496369755, 193275419, 4282361863, 3775550443, 4285664399, 2099488911, 2628534215, 2432263179, 3468160367, 1671973155, 2938398207, 3540562715, 3610544975, 2888893179, 289531399, 3114637431, 518482127, 2262440907, 3616476143, 2966216843, 4251779999, 4063592799, 3698910747, 1227503963, 3503973859, 3549089463, 2110776455, 3619754571, 1083848987, 511566439, 2400343107, 509034383, 719893503, 736382147, 2821535951, 4251845767, 24346707, 1239781443, 342252751, 2529639107, 2189157819, 1693232319, 740767787, 1791916739, 1765663095, 3156691819, 1991340499, 958996291, 2135223311, 2360682779, 1586995451, 3434584923, 2940990079, 3418758963, 1372936207, 2361497419, 3062180695, 208618619, 1725733583, 2796042723, 2487121779, 2082226923, 1299273727, 2383364727, 2507532991, 2064075831, 2887773999, 91937839, 2345075095, 1160117815, 1279637083, 3022557379, 570839187, 2230177543, 4006750547, 3932849719, 1617229291, 3249563659, 2877452527, 1439624795, 1425145927, 4241556719, 2877537811, 360631835, 3147736067, 2510674783, 342347975, 1421385619, 3768199279, 1318995403, 3263133419, 1305990095, 3181020099, 3472685135, 744958171, 2157270675, 1758501899, 4258829695, 116770123, 1891184931, 3632605511, 3405999467, 2858780831, 3030614027, 2752130379, 1087607031, 3350849903, 587682867, 3301124123, 4211664671, 2369171623, 3587401147, 3947184935, 516717579, 3742202675, 1851980795, 227105651, 1391894051, 3260881755, 1009078155, 2802669691, 4033409119, 433501371, 4206426979, 3379776495, 4153948811, 644090875, 4243668359, 988525183, 1811852355, 2466848203, 3084493955, 205252071, 4229791387, 1831448975, 3735604355, 867719339, 1774274559, 1823165563, 1632967647, 1972451915, 3255949783, 2722118247, 2123587763, 1799455635, 3441860939, 693811531, 4040594327, 1742319319, 2831120575, 135486615, 1559567959, 1039915323, 3023152723, 251345179, 148567, 561704047, 1355954615, 774087803, 3389528703, 3172982151, 2010490411, 682459251, 831882811, 894211647, 485954151, 1232061267, 2425232455, 2082951859, 2856803571, 3663214727, 951129247, 1740109903, 132967471, 1617389483, 1786056099, 1957601263, 307243539, 1674840935, 2545534311, 345789087, 40261811, 2618050483, 4175315647, 3355004779, 2940366139, 3242406531, 3578944239, 3813672983, 3459271219, 22162063, 459733307, 3379387139, 3590111659, 825938783, 2159217487, 1316209887, 996150787, 890431487, 4262164051, 1214711407, 1816814699, 2335696871, 2880406955, 711958555, 2719959847, 2084011139, 4261913603, 1509182503, 1304818507, 3083699111, 2921789095, 4059129203, 2261760407, 1019563015, 691014667, 1906147835, 1293438347, 869348659, 1407176107, 1069531619, 4254787791, 4134886535, 1373096587, 2096955731, 4094999907, 690372671, 1453114163, 4028140907, 1641215219, 1350582919, 4065335079, 1534430891, 518110787, 2903886559, 1413585223, 3780207603, 1087524403, 267967491, 2274571987, 317454635, 1324996979, 1603459087, 3785641383, 2015622987, 2757435339, 2566602983, 437354795, 1244832131, 4082921635, 1279038511, 3109649059, 3956011511, 3729148359, 4006633543, 2269454211, 3100998907, 3679214471, 3567774563, 1390545435, 3722311443, 1618760531, 4263609743, 1878156847, 3496278231, 3462553203, 3935173651, 815165179, 2364603439, 3056554631, 3292704623, 1935625451, 913462431, 768192155, 3425228515, 2531263131, 3466160667, 393912671, 1216565991, 3927150203, 3959205303, 2068956867, 2839465691, 3755156651, 2713597355, 3787241259, 2296682235, 3640042063, 2998070943, 759656435, 3110694359, 3503100635, 3450134095, 2575894791, 159914251, 4243956503, 567405463, 233418683, 3518999711, 579176879, 4275154795, 809789459, 3542304003, 2086175627, 273000439, 3133213731, 3004848287, 3398657631, 2016671219, 3199768015, 1893875655, 14853507, 71338819, 1863272287, 2168945379, 2546033459, 1359997375, 2142916243, 2271557579, 147465163, 4129300455, 3176739887, 748664399, 4007139707, 3552421535, 3377517603, 1916734431, 154486351, 639997259, 2318596467, 2650661439, 880545171, 1373252063, 1181523847, 539868359, 2746625087, 3433227679, 1129816391, 4231348283, 136905363, 1037136787, 2955894967, 1287022619, 2597542539, 137931507, 1380487135, 2723278723, 2047412999, 2868363691, 3484052783, 1873140843, 100908955, 1357269667, 731950535, 2473250863, 3131911443, 2332831151, 3460816211, 2484543219, 1757051979, 4131685919, 964586567, 1491285039, 2043301583, 1022473039, 3760969783, 1339500983, 793695803, 2389607883, 27021503, 811344423, 3768866691, 1475327619, 1537286715, 928074503, 920200651, 1345655299, 3652588403, 2719602327, 1380240035, 1965531495, 2029875511, 2924974999, 4036335655, 1087524587, 2165394963, 4003451291, 1479514511, 381466771, 1870885027, 1441016415, 1878239283, 1068056663, 3112319919, 1412949931, 2963965167, 3920140127, 171582467, 2256369035, 4122074907, 2694373175, 1996786523, 2270411423, 3174163271, 2874411583, 106757447, 3239387103, 3907239579, 4207727003, 4030947407, 1383952795, 1150338279, 2545698731, 2272496159, 687969107, 4134521323, 3108649663, 3200292007, 659716963, 2075117831, 640342235, 274215331, 2081372775, 3617568015, 3404302363, 731879075, 1643428895, 3797615119, 2870549439, 177581563, 3236858451, 2246737003, 2984110335, 1663474831, 163582899, 1146596991, 2929724191, 2373264991, 4154828407, 3269559475, 1648396579, 2604009939, 2125905063, 3199930127, 3852996587, 2647886839, 3189753023, 1571583591, 179995943, 585830579, 4110245259, 4093758547, 3840380751, 769505107, 4028728695, 1857689667, 2754615215, 4200375427, 3394239015, 1915150747, 917880031, 1116941215, 762735727, 2688364067, 536358899, 1448960639, 3873544039, 3111855623, 2059627635, 2118688135, 1095427123, 2786290667, 2227284839, 1065489927, 300508263, 217263015, 1965532363, 1345493259, 2106698463, 806804979, 3324239447, 3532560043, 1877703595, 3126131995, 1185144379, 1822913023, 375985031, 3888797751, 301865295, 2327875939, 280603419, 2150203883, 1899477827, 2453635715, 4210793479, 1392572831, 3671316987, 4031741819, 1005088607, 263849407, 1357966923, 1763681923, 2844118839, 1389549243, 3328892619, 4268972667, 1275429119, 622784523, 162478771, 305516291, 251542735, 1093471599, 2021624271, 2592797463, 663454467, 4225009607, 844761039, 2445117527, 4008735419, 1081352511, 2921013895, 3809959799, 3139127099, 4221682995, 3743369327, 402177411, 672237707, 403776223, 3624409531, 3466972627, 2005484183, 2532965591, 3865638579, 1041130615, 1874038915, 1811287379, 2254730695, 508811767, 595992999, 4108318087, 3625298875, 630359535, 475749255, 3194086415, 2099453967, 1097882715, 1972564059, 3079957091, 3216907907, 2904136543, 484634527, 1297091459, 1019866439, 1213745255, 3007335523, 1022131879, 3874779863, 1040940119, 593550247, 3555293967, 1394063003, 924071791, 657194179, 3962472595, 3352620527, 1294872159, 1370862063, 3634560027, 1103517803, 1107958643, 349124207, 847127883, 2312199671, 2893449619, 2506659971, 3898131515, 1249835079, 2912291795, 3182501363, 4276031491, 3320300723, 3691841651, 1151591783, 1325770763, 2470024743, 469963155, 4228332827, 1667500119, 4084457323, 2954667663, 2451322947, 2313918667, 3635948819, 1580073807, 1795564507, 3619994167, 1251831707, 1615220455, 3476126023, 2122468015, 3670871487, 3329389339, 385609819, 490508091, 310474559, 2419444695, 3320962171, 96171267, 2486568823, 148622599, 2561244099, 524390267, 2066148279, 4055946611, 4012783139, 99197491, 1211541039, 2921074371, 3793303943, 3716259839, 3570887107, 557336387, 540761783, 2600425811, 3002405967, 1073020715, 1409510215, 2828661819, 2506318903, 1904582739, 3282820943, 2784185047, 2428315727, 2292692887, 4080760243, 633896999, 3984255811, 1370050819, 4063421839, 1788241543, 1110225231, 2676295171, 2458314115, 1928213115, 2284988875, 3535586767, 1057234903, 591661063, 1894884767, 2494178843, 4131087471, 3812370251, 575641527, 3239624555, 373227219, 510717655, 1433936423, 2701373839, 1293211495, 3374884383, 994993535, 1067694355, 4249866667, 1894009439, 1740567619, 2189724879, 2868799811, 3463490855, 32048823, 1677023555, 1013572327, 2029582383, 175190891, 151630511, 3326102735, 3985817027, 2331579159, 1049624839, 3681904483, 696652263, 935651391, 1506444955, 1566775363, 1370525479, 2234560803, 130341331, 3244166311, 1680156607, 254674911, 1471925779, 2063801675, 1408211159, 112690999, 3726065451, 208188551, 793973151, 225793343, 2935502599, 3364863203, 2820608039, 2375328423, 458675491, 3430774799, 59616611, 2482356927, 2842702655, 3275885799, 3208825903, 59710187, 1133360107, 3974203911, 2297973847, 2743180827, 889159483, 2327345111, 1153331607, 3007667683, 3565352887, 2441783259, 1811481559, 1073529675, 3890109487, 589265523, 3269380031, 1862105995, 2309457555, 2085309219, 893056351, 2319099575, 1278856675, 2375613611, 1794653915, 1159975135, 2541638007, 746129151, 1591511735, 2560819303, 3977388191, 3251906975, 3952267947, 1895404811, 584274759, 1637408751, 2690854327, 407309739, 1620754527, 2905764403, 4281873443, 48400619, 3358724631, 488252199, 3078677691, 760323355, 2880427763, 2733775119, 3491913543, 2079506659, 217997511, 3528633659, 3989811275, 3296502011, 176098511, 2102501007, 2928407091, 227350339, 2577031031, 382456547, 744074255, 2866991583, 2891639719, 3589629095, 3222774439, 809307903, 1001988503, 1287723395, 1346225471, 1392114043, 3890570475, 755060299, 1444249943, 2036157443, 1335387139, 340149119, 4170117775, 1301165967, 2123611343, 3641271703, 898389363, 3802026723, 1350710175, 2672619559, 1884231803, 3323840455, 2540403623, 1119355175, 312932375, 4178372695, 1525272155, 687494271, 3459204655, 3274511059, 978871771, 1605383191, 3567165915, 2389230131, 1006314651, 2251222499, 1727036151, 3236185863, 3254384839, 109194127, 506372019, 1353184843, 2398697359, 2716666863, 3287464503, 1276004047, 672567347, 3626206259, 4163203087, 1882450699, 3093259631, 2361324411, 2198462611, 3133881987, 327591739, 3822482575, 3746165807, 440729127, 807417139, 3686803151, 1227922575, 3766340403, 1114725911, 951122595, 2821531383, 1913888339, 143123907, 2137441143, 1758430727, 1036984595, 3560492679, 2597477535, 749944867, 697623039, 606760631, 109427875, 2256825187, 442399595, 3325133979, 3247779083, 1428587863, 1007273943, 2775212219, 3625840443, 1326343539, 3065712311, 2335827259, 3491775539, 2523097995, 706646891, 116465407, 390801527, 1426390187, 716811375, 3542005755, 2573833059, 4038479543, 757384163, 1319256883, 1458179955, 2238226051, 1350921335, 1241418267, 2765170219, 2820486863, 3124801027, 1813938967, 2335965495, 3708404463, 2742434715, 361230271, 1292997787, 153340575, 1362035371, 241705115, 770720355, 1654726463, 3003642783, 4078430667, 293560547, 311264731, 3078071995, 996512951, 2010703095, 159333415, 4254455419, 1560979811, 4059120723, 1664689803, 873247267, 2781020471, 1565811127, 4026313927, 2711484387, 2266588631, 1015494539, 3704029883, 2045552927, 787304779, 1236390259, 930877111, 2356549855, 2554649471, 3420025347, 1583360051, 502995971, 2381314331};
ui table;
const int Block = 1e6;
(ll tar)
ui gettable{
if (tar == 0)
return 0;
int l = 0, r = 1e9 + 1;
while (r - l > 1)
{
int mid = (l + r) >> 1;
if ((ll)mid * (ll)(mid + 1) <= tar)
= mid;
l else
= mid;
r }
return table[l / Block] + query(l / Block * Block, l + 1);
}
void solve()
{
, r;
ll l("%lld%lld", &l, &r);
scanfint posl = lower_bound(bigans.begin(), bigans.end(), pair<ll, ui>{l, ui(0)}) - bigans.begin();
int posr = lower_bound(bigans.begin(), bigans.end(), pair<ll, ui>{r + 1, ui(0)}) - bigans.begin();
= bigans[posl].second - bigans[posr].second;
ui ans //cout << gettable(r) - gettable(l - 1) << endl;
//cout << ans << endl;
+= (gettable(r) - gettable(l - 1));
ans ("%u\n", ans);
printf}
signed main()
{
(maxn);
PreEulerTable();
preint t;
("%d", &t);
scanfwhile (t--)
{
();
solve}
}
请我喝杯咖啡吧~