题解:CF2252E Generational Triplets
一种并非数位 dp 的做法。我们设
我们不妨设公差为
考虑
若 a,d 均为偶数
不妨设
若 a,d 均为奇数
不妨设
再次对
若 x 为偶数
令
若 x 为奇数
同样地,令
综合以上两种讨论,我们得到
综上,我们有递推公式
#include<bits/stdc++.h>
#include<bits/extc++.h>
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define int long long
#define gc getchar
//char buf[1<<20],*p1,*p2;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
#define R read()
using namespace std;
int read()
{
int x=0,f=1;
char c=gc();
while(c>'9'||c<'0'){if(c=='-') f=-1;c=gc();}
while(c>='0'&&c<='9') x=(x<<1)+(x<<3)+c-48,c=gc();
return x*f;
}
void write(int x,char xx)
{
static int st[35],top=0;
if(x<0){x=-x;putchar('-');}
do
{
st[top++]=x%10,x/=10;
}while(x);
while(top) putchar(st[--top]+48);
putchar(xx);
}
#define mod 1000000007
int lp(int x,int y){return x+y>=mod?x+y-mod:x+y;}
void pl(int &x,int y){x=lp(x,y);}
using namespace __gnu_pbds;
int n;
unordered_map<int,int>f;
int dfs(int n)
{
if(n<3) return 0;
if(f.count(n)) return f[n];
int k=n-1>>1,ans=lp(lp(dfs(n>>1),1),lp(dfs(k>>1),dfs(k-1>>1)));
return f[n]=ans;
}
void solve()
{
n=R,write(dfs(n),'\n');
}
int T=1;
signed main()
{
T=R;
while(T--) solve();
return 0;
}