Title link
Reference from: this
D. Dreamoon Likes Sequences
Topic:
Given a limit d and modulus mod, to find the number of arrays a that can be constructed to satisfy the condition, the following conditions need to be met:
The length of array a is greater than or equal to 1
array a
. The minimum value of strictly increasing array a is greater than or equal to 1, and the maximum value of array a is less than or equal to d.
For array a, construct an array b: when
i == 1: b [1] = a When [1]
i> 1: b [i] = b [i-1] ^ a [i]
array b strictly incrementsPractice: I am confused about the math idiot, I do n’t know where to start, so Baidu solves the problem
It's not easy to get this conclusion. This kind of mathematical foundation should be able to see this conclusion at a glance. It's still my dish
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define pi pair<int, int>
#define mk make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll n,mod,ans,f[35];
int main()
{
f[0]=1;
for(int i=1;i<=30;++i) f[i]=f[i-1]*2;
int _;cin>>_;while(_--)
{
scanf("%lld%lld",&n,&mod);
ans=1;
for(int i=0;i<=30;++i){
if(f[i]>n) break;
ans*=(min(f[i+1]-1,n)-(f[i]-1)+1);
ans%=mod;
ans+=mod;
ans%=mod;
}
ans-=1;
ans=(ans+mod)%mod;
printf("%lld\n",ans);
}
}
E. Drazil Likes Heap
I can't write it, the main question is too circumvent, search for the solution, and found the solution of this brother: this
what? This is called greed? I feel that this is called simulating according to the meaning of the question after understanding the meaning of the question
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=(1<<21)+100;
int a[N];
int get_id(int k)
{
if(!a[k<<1]&&!a[k<<1|1])
return k;
if(a[k<<1]>a[k<<1|1])
return get_id(k<<1);
else
return get_id(k<<1|1);
}
void dfs(int k)
{
if(!a[k<<1]&&!a[k<<1|1])
a[k]=0;
else if(a[k<<1]>a[k<<1|1])
{
a[k]=a[k<<1];
dfs(k<<1);
}
else
{
a[k]=a[k<<1|1];
dfs(k<<1|1);
}
}
int main()
{
int w;
cin>>w;
while(w--)
{
int h,g;
scanf("%d%d",&h,&g);
for(int i=1;i<1<<(h+1);i++)
a[i]=0;
for(int i=1;i<1<<h;i++)
scanf("%d",a+i);
vector<int>ans;
int limit=(1<<g)-1;
for(int i=1;i<1<<g;i++)
while(get_id(i)>limit)
{
ans.push_back(i);
dfs(i);
}
LL sum=0;
for(int i=1;i<1<<g;i++)
sum+=a[i];
printf("%lld\n",sum);
for(int i=0;i<ans.size();i++)
printf("%d ",ans[i]);
puts("");
}
return 0;
}