A
#include
using namespace std;
typedef long long ll;
#define lowbit(x) (x & (-x))
const int N = 1e5 + 10;
ll getnum(ll n, ll x, ll y) {
ll p = 0;
ll ans;
if(x <= y && x + y <= n + 1) {
p = x;
ans = 4*(p-1)*n - 4*(p-1)*(p-1) +1 + y-p;
} else if(x <= y && x+y >= n+1) {
p = n- y + 1;
ans = 4*(p-1)*n - 4*(p-1)*(p-1) + 1 + n-2*p + 1 + x - p;
}else if(x>=y && x+y >= n+1) {
p = n - x +1;
ans = 4*(p-1)*n - 4*(p-1)*(p-1) + 1 + 3*n-6*p + 3 - y + p;
}else if(x>=y && x+y <= n+1) {
p = y;
ans = 4*(p-1)*n - 4*(p-1)*(p-1) + 1 + 4*n-8*p + 4 - x + p;
}
return ans;
}
int n, m, p;
struct node {
int x, y;
ll val;
int op;
int pos;
bool operator <(const node &xx)const {
return x == xx.x ? op < xx.op : x < xx.x;
}
}q[N * 5], tmp[N * 5];
int len;
ll ans[N];
ll sum[N * 10];
void update(int x,ll val)
{
while(x<=1000000)
{
sum[x]+=val;
x+=lowbit(x);
}
}
int query(int x)
{
ll res=0;
while(x)
{
res+=sum[x];
x-=lowbit(x);
}
return res;
}
void sum_clear(int x)
{
while(x<=1000000)
{
sum[x]=0;
x+=lowbit(x);
}
}
void CDQ(int l,int r)
{
if(r-l<=1) return;
int mid=(r+l)>>1;
CDQ(l,mid);CDQ(mid,r);
int ll=l,rr=mid;
int cnt=0;
while(ll
B
#include
typedef long long ll;
const int N=1e6+11;
int phi[N];
ll a,b,mo;
void init(){
for(int i=1;i=c) a=a%c+c;
while(b){
if(b&1){
ans*=a;
if(ans>=c) ans=ans%c+c;
}
a*=a;
if(a>=c) a=a%c+c;
b>>=1;
}
return ans;
}
ll dfs(ll x,ll y,ll z){
if(z==0) return 1%y;
if(x%y==0) return poww(x,x,y);
return poww(x,dfs(x,phi[y],z-1),y);
}
int main(){
int t;
init();
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld",&a,&b,&mo);
printf("%lld\n",dfs(a,mo,b)%mo);
}
return 0;
}
F
#include
using namespace std;
const int N = 1e5 + 10;
struct node {
int l, r;
int val;
}tree[N * 22];
int n, m;
int root[N], tot;
int update(int pre, int l, int r, int pos) {
int cur = ++tot;
tree[cur] = tree[pre];
tree[cur].val++;
if(l == r) return cur;
int mid =(l + r) >> 1;
if(pos <= mid) tree[cur].l = update(tree[pre].l, l, mid, pos);
else tree[cur].r = update(tree[pre].r, mid + 1, r, pos);
return cur;
}
int query(int pl, int pr, int l, int r, int pos) {
if(l == r) {
return l;
}
int res = 0;
int mid = (l + r) >> 1;
if(pos <= r) {
if(pos >= mid + 1 && tree[tree[pr].r].val - tree[tree[pl].r].val > 0) res = max(res, query(tree[pl].r, tree[pr].r, mid + 1, r, pos));
if(tree[tree[pr].l].val - tree[tree[pl].l].val > 0)res = max(res, query(tree[pl].l, tree[pr].l, l, mid, pos));
} else {
if(tree[tree[pr].r].val- tree[tree[pl].r].val > 0) res = max(res, query(tree[pl].r, tree[pr].r, mid + 1, r, pos));
else if(tree[tree[pr].l].val - tree[tree[pl].l].val > 0)res = max(res, query(tree[pl].l, tree[pr].l, l, mid, pos));
}
return res;
}
int queryrk(int ll, int rr, int k) {
if(k == 1) return 0;
ll = max(ll, 1);
rr = min(rr, n);
int num = 0;
num = query(root[ll - 1], root[rr], 1, n, k - 1);
if(num > k) num = 0;
// cout << num << endl;
return num;
}
int ans[N],minx[N],a[N];
int main() {
int T;
scanf("%d", &T);
while(T--) {
tot = 0;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
root[i] = update(root[i - 1], 1, n, a[i]);
}
for(int i=1;i<=n;i++){
minx[a[i]] = queryrk(i - m, i + m, a[i]);
// if(minnx[a[i]] > i) minx[a[i]] = 0;
}
for(int i=1;i<=n;i++){
// printf("%d ",minx[i]);
if(minx[i] == 0) ans[i]=1;
else ans[i]=ans[minx[i]]+1;
}
// printf("\n");
for(int i=1;i<=n;i++) printf("%d%c",ans[i]," \n"[i==n]);
}
return 0;
}
H
#include
#include
#include
using namespace std;
typedef long long ll;
const int N=333,M=555;
const ll inf=1e18+7;
struct Side{
int v,ne;
ll w;
}S[M];
bool inq[N];
int n,sn,head[N];
ll dis[N];
void init(){
sn=0;
for(int i=0;i<=n;i++) head[i]=-1;
}
void add(int u,int v,ll w){
S[sn].w=w;
S[sn].v=v;
S[sn].ne=head[u];
head[u]=sn++;
}
ll spfa(int u,int v){
for(int i=0;i<=n;i++){
dis[i]=inf;
inq[i]=false;
}
queue q;
q.push(u);
dis[u]=0;
inq[u]=true;
int x,y;
while(!q.empty()){
x=q.front();
q.pop();
inq[x]=false;
for(int i=head[x];~i;i=S[i].ne){
y=S[i].v;
if(dis[y]>dis[x]+S[i].w){
dis[y]=dis[x]+S[i].w;
if(!inq[y]) q.push(y);
}
}
}
return dis[v];
}
int main(){
int t,m,u,v;
ll w;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
while(m--){
scanf("%d%d%lld",&u,&v,&w);
add(u,v,w);
}
for(int i=0;i<6;i++){
scanf("%d%d",&u,&v);
w=spfa(v,u);
printf("%lld\n",-w);
add(u,v,-w);
}
}
return 0;
}