LightOJ 1370 Bi-shoe and Phi-shoe 欧拉函数+线段树

分析:对于每个数,找到欧拉函数值大于它的,且标号最小的,预处理欧拉函数,然后按值建线段树就可以了

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
typedef long long LL;
const int N = 1e6+5;
const int INF=0x3f3f3f3f;
int phi[N],c[N<<2];
void pushup(int rt){
  c[rt]=min(c[rt<<1],c[rt<<1|1]);
}
void add(int rt,int l,int r,int pos,int t){
  if(l==r){
     c[rt]=min(c[rt],t);
     return;
  }
  int m=(l+r)>>1;
  if(pos<=m)add(rt<<1,l,m,pos,t);
  else add(rt<<1|1,m+1,r,pos,t);
  pushup(rt);
}
int get(int rt,int l,int r,int x,int y){
  if(x<=l&&r<=y)return c[rt];
  int m=(l+r)>>1;
  int ans=INF;
  if(x<=m)ans=min(ans,get(rt<<1,l,m,x,y));
  if(y>m)ans=min(ans,get(rt<<1|1,m+1,r,x,y));
  return ans;
}
int main(){
    phi[1]=1;
    for(int i=2;i<=N-2;++i){
      if(!phi[i]){
        for(int j=i;j<=N-2;j+=i){
          if(!phi[j])phi[j]=j;
          phi[j]=phi[j]/i*(i-1);
        }
      }
    }
    memset(c,INF,sizeof(c));
    for(int i=2;i<=N-2;++i)
      add(1,1,N-2,phi[i],i);
    int T,cas=0;
    scanf("%d",&T);
    while(T--){
       int n;
       scanf("%d",&n);
       LL ans=0;
       for(int i=1;i<=n;++i){
         int x;
         scanf("%d",&x);
         ans+=get(1,1,N-2,x,N-2);
       }
       printf("Case %d: %lld Xukha\n",++cas,ans);
    }
    return 0;
}

时间: 2024-08-27 23:45:32

LightOJ 1370 Bi-shoe and Phi-shoe 欧拉函数+线段树的相关文章

loj1370(欧拉函数+线段树)

传送门:Bi-shoe and Phi-shoe 题意:给出多个n(1<=n<=1e6),求满足phi(x)>=n的最小的x之和. 分析:先预处理出1~1e6的欧拉函数,然后建立一颗线段树维护最大值,对于每个n询问大于等于n的最左边下标. #pragma comment(linker,"/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <

BZOJ 3813 奇数国 欧拉函数+线段树+乘法逆元

题目大意:给出一个序列,支持修改操作,求这个序列连续一段的乘积的欧拉函数.每个数的最大质因子不超过281. 思路:φ(n) = n * (1 - 1 / p1) * (1 - 1 / p2) * (1 - 1 / p3) * (1 - 1 / p4)--*(1 - 1 / pn) = n  / (p1 * p2 * p3 * -- * pn) * ((p1 - 1) * (p2 - 1) * (p3 - 1) * -- * (pn - 1)) 于是这个东西只需要维护一下区间中的指数,用bitse

Ligh OJ 1370 Party All the Time (欧拉函数 +素数打表)

1370 - Bi-shoe and Phi-shoe PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for

BZOJ 4034 树上操作(树的欧拉序列+线段树)

刷个清新的数据结构题爽一爽? 题意: 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a . 操作 3 :询问某个节点 x 到根的路径中所有点的点权和. 注意到操作3,询问x到根的路径之间点权和,容易发现这就是欧拉序列中的前缀和. 所以按照树的欧拉序列建线段树,然后操作1就变成单点修改,操作2,就变成了区间内某些点+a,某些点-a,也容易用tag标记

FZU 1759 欧拉函数 降幂公式

Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000). Input There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a singl

XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】

1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 45  Solved: 8[Submit][Status][Web Board] Description 刘备(161年-223年6月10日),字玄德,东汉末年幽州涿郡涿县,西汉中山靖王刘胜的后代.刘备一生极具传奇色彩,早年颠沛流离.备尝艰辛最终却凭借自己的谋略终成一方霸主.那么在那个风云激荡的年代,刘备又是如何从一个卖草鞋的小人物一步一步成为蜀汉的开国皇帝呢

UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)

题意: 求sum{gcd(i, j) | 1 ≤ i < j ≤ n} 分析: 有这样一个很有用的结论:gcd(x, n) = i的充要条件是gcd(x/i, n/i) = 1,因此满足条件的x有phi(n/i)个,其中Phi为欧拉函数. 所以枚举i和i的倍数n,累加i * phi(n/i)即可. 1 #include <cstdio> 2 typedef long long LL; 3 4 const int maxn = 4000000; 5 6 int phi[maxn + 10]

poj3696 快速幂的优化+欧拉函数+gcd的优化+互质

这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((10^x)-1)*(8/9) 那么有m=((10^x)-1)*(8/9)=k*L,answer即满足条件的最小的x 性质1:若ax=by且a和b互质,那么说明a中没有任何b的质因子,b的质因子一定都在x里.所以x是b的倍数. 所以先想方设法在等式中构造两个互质的数以便化简.我们取p=8/gcd(8,L

欧拉线性筛法求素数(顺便实现欧拉函数的求值)

我们先来看一下最经典的埃拉特斯特尼筛法.时间复杂度为O(n loglog n) int ans[MAXN]; void Prime(int n) { int cnt=0; memset(prime,1,sizeof(prime)); prime[0]=prime[1]=0; for(int i=2;i<n;i++) { if(vis[i]) { ans[cnt++]=i;//保存素数 for(int j=i*i;j<n;j+=i)//i*i开始进行了稍微的优化 prime[j]=0;//不是素