bzoj4028[HEOI2015]公约数数列

http://www.lydsy.com/JudgeOnline/problem.php?id=4028

分块。

好吧,其实gcd和xor并没有什么联系,我一直在找它们之间的联系,浪费了好多时间。。。。。。

将数列分成$sqrt{N}$块,记第i个块的左端为l[i],右端为r[i]。

每个位置j记住到所在块的左端的最大公约数g[j]和异或和x[j]。

对于询问操作:

设读入的数为H。

我们从左到右枚举块(不妨设当前枚举的块为i),记lastgcd表示块i左端l[i]之前(不包含l[i])所有数的最大公约数,lastxor表示块i左端之前(不包含l[i])所有数的异或和为lastxor。

如果块i右端r[i]之前(包含r[i])所有数的最大公约数等于lastgcd,即gcd(g[r[i]],lastgcd)==lastgcd:

我们发现前缀的最大公约数是单调不上升的,如果gcd(g[r[i]],lastgcd)==lastgcd,说明g[l[i]...r[i]]都等于lastgcd。

所以我们要在块j中找到最小的位置j,满足:

$lastgcdast (x[j] xor lastxor)=H$

化简得:

$xor[j]=(frac{H}{lastgcd}) xor lastxor$

这个我们可以用map快速找到。

如果gcd(g[r[i]],lastgcd)!=lastgcd:

这个时候我们只能用$O(sqrt{N})$暴力扫一遍。

但是我们发现前缀的最大公约数是单调不上升的,并且每次减小至少要除以2。

所以这种情况的出现的次数不超过$log_{2}N$次。

所以询问的复杂度是$O(sqrt{N}log_{2}N)$

对于修改操作:

直接在所在的块重新求一次。

加上放进map的时间,所以修改的复杂度是$O(sqrt{N}log_{2}N)$

所以总的时间复杂度是$O(Qsqrt{N}log_{2}N)$

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

inline int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
inline LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
  }

const int maxN=100000;
const int maxcnt=350;

int N,Q;
int cnt,len,id[maxN+100],g[maxN+100],x[maxN+100];
int l[maxcnt+10],r[maxcnt+10];
map<int,int> S[maxcnt+10];
int a[maxN+100];

inline int gcd(int a,int b){return b==0?a:gcd(b,a%b);}

int main()
  {
      freopen("bzoj4028.in","r",stdin);
      freopen("bzoj4028.out","w",stdout);
      int i,j;
      N=gint();
      len=int(sqrt(DB(N)));
      cnt=0;
        re(i,1,N)
          {
              if((i-1)%len==0)r[cnt]=i-1,l[++cnt]=i;
                id[i]=cnt;
            }
        r[cnt]=N;
      re(i,1,N)a[i]=gint();
      re(i,1,cnt)
            re(j,l[i],r[i])
              {
                g[j]=(j==l[i])?a[j]:gcd(g[j-1],a[j]);
                x[j]=(j==l[i])?a[j]:x[j-1]^a[j];
                if(S[i].find(x[j])==S[i].end())S[i][x[j]]=j;
              }
        Q=gint();
        while(Q--)
          {
              char s[20];int pos,val,lastgcd,lastxor,res;LL H;
              SF("%s",s);
              switch(s[0])
                {
                    case 'M':
                        pos=gint()+1,val=gint();
                        i=id[pos];
                        re(j,pos,r[i]) if(S[i][x[j]]==j) S[i].erase(x[j]);
                            a[pos]=val;
                            re(j,pos,r[i])
                              {
                                    g[j]=(j==l[i])?a[j]:gcd(g[j-1],a[j]);
                                    x[j]=(j==l[i])?a[j]:x[j-1]^a[j];
                                    if(S[i].find(x[j])==S[i].end())S[i][x[j]]=j;
                                }
                    break;
                    case 'Q':
                        H=gll();
                        res=0;
                        lastgcd=lastxor=0;
                        re(i,1,cnt)
                          {
                            if(gcd(lastgcd,g[r[i]])==lastgcd)
                              {
                                  if(H%LL(lastgcd)==0)
                                    {
                                        LL temp=LL(lastxor)^(H/LL(lastgcd));
                                        if(temp<=LL(2147483647))
                                        if(S[i].find(int(temp))!=S[i].end())
                                          res=S[i][int(temp)];
                                    }
                              }
                            else
                              {
                                re(j,l[i],r[i])
                                              if(LL(gcd(lastgcd,g[j]))*LL(lastxor^x[j])==H){res=j;break;}
                              }
                                    if(res)break;
                            lastgcd=gcd(lastgcd,g[r[i]]);
                            lastxor^=x[r[i]];
                                }
                            if(res)PF("%d
",res-1);else PF("no
");
                    break;
                }
          }
        return 0;
    }
View Code
原文地址:https://www.cnblogs.com/maijing/p/4765730.html