目录
Xor sum(字典树)
题意
给定n,k; 下方给出n个数的序列,求出长度最短且序列最左边序号最小的序列,
子序列的异或和要大于k;
思路:
01字典树可以解决异或和问题。
可以将给定序列,可以求出前缀异或和。然后问题就变成了
两个异或前缀和(区间的序列异或和)>k。
我们在建立字典树的时候,后来的数的序号要覆盖前面的。用mr[]数组存取。
边建树边询问。
void 询问(int x)
{
int res(序号)=0;
int flag(标记)=1;
for(int i=31;i>=0;i--)
{
int v1=x的二进制左移i位
int v2=k的二进制左移i位
if(v2==0)当前k的左移i位为0,只要异或值为1就一定大于,异或值为0需要继续判断。
{
if(v1==0&&字典树右子树存在)//异或值为1>v2
{
res=max(res,序列号mr[字典树右子树节点序号]);
//右子树的子树所有节点所存储的序列号最大也就是右子树存储的序列号。
//不用再继续判断右子树的子树。判断字典树的左子树;
if(左子树存在)
{
节点号=左子树存储的节点号。
}
else
{
flag=1;
break;
}
}
else if(v1==1&&字典树左子树存在)
{
与(v1==0&&字典树右子树存在)同理。
}
else if(v1==0&&字典树左子树存在)//异或值为0,不能做出判断需要继续往下左子树的子树判断
{
节点号=左子树存储的节点号。
}
else if(v1==1&&字典树右子树存在)//异或值为0,不能做出判断需要继续往下右子树的子树判断
{
节点号=右子树存储的节点号。
}
}
else
{
if(v1==1&&字典树左子树存在)异或值为1,不能做出判断需要继续往下左子树的子树判断
{
节点号=左子树存储的节点号。
}
else if(v1==0&&字典树右子树存在)异或值为1,不能做出判断需要继续往下右子树的子树判断
{
节点号=右子树存储的节点号。
}
}
}
if(flag==1) res=max(res,mr[rt]);
return res;
}
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
typedef pair<int,int> PII;
#define re register
const int inf=0x3f3f3f3f;
const double PI = acos(-1);
const int mod=1e9+7;
const int N=101010;
int n;
int ans,cnt;
pair<int,int>pa;
LL a[101011],k;
int tree[100000005][2], mr[100000005];
void add(LL qq,int p)
{
int len=31,rt=0;
bool x;
for(int i=len;i>=0;i--)
{
mr[rt]=p;
x=qq&(1LL<<i);
if(!tree[rt][x]) tree[rt][x]=++cnt;
rt=tree[rt][x];
}
mr[rt]=p;
}
int que(LL x)
{
int res=-1,flag=1,rt=0;
for(int i=31;i>=0;i--)
{
bool v1=x&(1LL<<i);
bool v2=k&(1LL<<i);
if(v2==0)
{
if(v1==0&&tree[rt][1])
{
res=max(res,mr[tree[rt][1]]);
if(tree[rt][0])
{
rt=tree[rt][0];
}
else
{
flag=0;
break;
}
}
else if(v1==1&&tree[rt][0])
{
res=max(res,mr[tree[rt][0]]);
if(tree[rt][1])
{
rt=tree[rt][1];
}
else
{
flag=0;
break;
}
}
else if(v1==0&&tree[rt][0])
{
rt=tree[rt][0];
}
else if(v1==1&&tree[rt][1])
{
rt=tree[rt][1];
}
}
else
{
if(v1==0&&tree[rt][1]) rt=tree[rt][1];
else if(v1==1&&tree[rt][0]) rt=tree[rt][0];
else
{
flag=0;
break;
}
}
}
if(flag==1) res=max(res,mr[rt]);
return res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
//memset(tree,0,sizeof tree);
//memset(mr,0,sizeof mr);
ans=1e9;
pa=make_pair(0,0);
scanf("%d %lld",&n,&k);
cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
a[i]=a[i-1]^a[i];
}
add(0,0);
int left;
for(int i=1;i<=n;i++)
{
add(a[i],i);
left=que(a[i]);
if(left==-1) continue;
if(i-left<ans)
{
ans=i-left;
pa=make_pair(left+1,i);
}
}
if(ans==1e9)
{
printf("-1\n");
}
else
{
printf("%d %d\n",pa.first,pa.second);
}
for(int i=0;i<=cnt;i++) mr[i] = tree[i][0] = tree[i][1] = 0;
}
return 0;
}
zoto(莫队+分块)
题意:
给定n个数,为f[i]数组,(i,f[i])为一个点;
m次询问,询问矩阵中x1,y1,x2,y2中有多少个点。y坐标是不同的。
简化问题:在序列x1到x2中,大于等于y1小于等于y2的不同的数有多少个。
思路;
对于不同的区间 [x0 , x1]可以用莫队来移动,先确定一个区间,每次移动我们需要修改对应点的个数,
如果这个数的个数从无到有 ,或者从有到无 需要修改答案的权值 如果用数状数组修改的话会多一个
log 最坏的复杂度会有 O(n sqrt(n) log(n))。而用分块的话可以 o(1) 修改,
最后的复杂度接近 O(n sqrt(n) )
代码:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
typedef pair<int,int> PII;
#define re register
const int inf=0x3f3f3f3f;
const double PI = acos(-1);
const int mod=1e9+7;
const int maxn=1e5+7;
int n,m,k,apart,curl=1,curr;
int a[maxn],cnt[maxn];
int sum[maxn],val[maxn],l[maxn],r[maxn],num;
int block,belong[maxn];
int T,ans[maxn];
struct node
{
int l,r,num;
int val_l,val_r;
}G[maxn];
bool cmp(node x,node y)
{
if(x.l/apart==y.l/apart)
return x.r<y.r;
else
return x.l<y.l;
}
void add(int x)
{
cnt[a[x]]++;
if(cnt[a[x]]==1)
{
val[a[x]]++;
sum[belong[a[x]]]++;
}
}
void del(int x)
{
cnt[a[x]]--;
if(cnt[a[x]]==0)
{
val[a[x]]--;
sum[belong[a[x]]]--;
}
}
void build()//分块建立
{
block=sqrt(n);
num=n/block;if(n%block) num++;
for(int i=1;i<=num;i++)
{
l[i]=(i-1)*block+1;
r[i]=min(n,i*block);
}
for(int i=1;i<=n;i++)
{
belong[i]=(i-1)/block+1;//第i个数属于那一块
sum[belong[i]]+=val[i];
}
}
int ask(int L, int R)//询问
{
int ans=0;
int s=belong[L],e=belong[R];
if(s==e)
{
for(int i=L;i<=R;i++)
{
ans+=val[i];
}
return ans;
}
for(int i=L;i<=r[s];i++)
{
ans+=val[i];
}
for(int i=s+1;i<e;i++)
{
ans+=sum[i];
}
for(int i=l[e];i<=R;i++)
{
ans+=val[i];
}
return ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
memset(sum,0,sizeof(sum));
memset(val,0,sizeof(val));
memset(cnt,0,sizeof(cnt));
curl=0,curr=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]++;
}
apart=sqrt(n);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d %d",&G[i].l,&G[i].val_l,&G[i].r,&G[i].val_r);
G[i].val_l++;
G[i].val_r++;
G[i].num=i;
}
build();
sort(G+1,G+m+1,cmp);
for(int i=1;i<=m;i++)
{
int l=G[i].l,r=G[i].r;
while(curl<l)
{
del(curl++);
}
while(curl>l)
{
add(--curl);
}
while(curr<r)
{
add(++curr);
}
while(curr>r)
{
del(curr--);
}
ans[G[i].num]=ask(G[i].val_l,G[i].val_r);
}
for(int i=1;i<=m;i++)
{
printf("%d\n",ans[i]);
}
}
return 0;
}
Pass!(广义斐波那契数列求通项+BSGS)
题意:
有n个人在传球,从1号开始传球,每次只能传给其他的人,设传了t次球后球回到1的方案数为cnt ,
求最小的t其满足此时的 cnt mod 998244353=x,如果无解输出−1
题解:
我们可以假设f(i)为第i秒球在1号手中的方案数。第i-1秒球肯定不能在1号手中;
我们讨论第i-2秒球在1号手中/不在1号手中
1、在1号手中,方案数就等于f(i-2)*(n-1),f(i-2)为第i-2秒球在1号手中的方案数,(n-1)为
下一步第i-1秒球可以传给n-1个人(1号除外);
2、不在1号手中,方案数等于(n-2)*f(i-1),第i-1秒球肯定不能在1号手中,第i-1秒球不在1号
手中的方案数f(i-1)*(n-2), f(i-1)为第i-1秒在1号手中的方案数,(n-2)为下一次传球可以1
号可以传给除了一号和当前拿球的人
得出递推式:f(i)=(n-1)*f(i-2)+(n-2)*f(i-1);
通过广义斐波那契数列求通项 f(t)
求通项步骤:
然后就可以运用BSGS求出当t为奇数时最小值res,t为偶数时最小值ans,
二者比较求最小值
BSGS是求给定质数 p ,整数 a , b求满足下式的最小自然数 x:
代码:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
typedef pair<int,int> PII;
#define re register
inline int read()
{
int ans=0;
char last=' ',ch=getchar();
while(ch<'0'|ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans;
return ans;
}
int buf[105];
inline void write(int i)
{
int p = 0;
if(i == 0) p++;
else while(i)
{
buf[p++] = i % 10;
i /= 10;
}
for(int j = p-1; j >= 0; j--) putchar('0' + buf[j]);
}
const int inf=0x3f3f3f3f;
const double PI = acos(-1);
int qmi(int m, int k, int p)
{
int res = 1 % p, t = m;
while (k)
{
if (k&1) res = (LL)res * t % p;
t = (LL)t * t % p;
k >>= 1;
}
return res;
}
int gcd(int a,int b)
{
return b ? gcd(b,a%b) : a;
}
const int N=100010;
const int mod=998244353;
unordered_map<int,int>Hash;
LL bsgs(LL a,LL b,LL p)
{
Hash.clear();
a=a%p;
b=b%p;
if(b==1) return 0;
int k=sqrt(p)+1;
for(int i=0,j=b%p;i<k;i++)
{
Hash[j]=i;
j=(LL)j*a%p;
}
int ak=qmi(a,k,mod);
for(int x=1,num=ak;x<=k;x++)
{
if(Hash.count(num)) return x*k-Hash[num];
num=(LL)num*ak%p;
}
return -1;
}
void solve()
{
LL n,x;
scanf("%lld %lld",&n,&x);
if(x==1) puts("0");
else if(x==0) puts("1");
else
{
LL tmp=x;
x=1LL*x*n%mod+(n-1);//t为奇数
x=(x+mod)%mod;
LL res=bsgs(n-1,x,mod);
if(res%2==0) res=-1;
x=tmp;
x=1LL*x*n%mod-(n-1);//t为偶数
x=(x+mod)%mod;
LL ans=bsgs(n-1,x,mod);
if(ans%2==1) ans=-1;
if(res==ans&&res==-1) puts("-1");
else if(res==-1) printf("%lld\n",ans);
else if(ans==-1) printf("%lld\n",res);
else printf("%lld\n",min(res,ans));
}
}
int main()
{
int t;
t=1;
scanf("%d",&t);
while(t--)
{
solve();
}
return 0;
}