A 考察组合数学 dp
f[i]为前i个人的组成方案,计算时用组合数学方法计算
# include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long LL;
const int mod=998244353,N=1e6+10;
int fac[N],ifac[N],f[N];
LL C(int n, int m)
{
return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
LL fpow(LL a, LL b)
{
LL res = 1;
while(b)
{
if(b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
void init(int n)
{
fac[0]=1;
for(int i=1;i<=n;i++)
{
fac[i]=fac[i-1]*i%mod;
}
ifac[n] = fpow(fac[n], mod - 2);
for(int i=n;i>=1;i--)
{
ifac[i-1]=ifac[i]*i%mod;
}
f[0]=1;
f[1]=0;
f[2]=1;
f[3]=1;
for(int i=4;i<=n;i++)
{
f[i]=C(i-1,1)*f[i-2]%mod;
f[i]+=C(i-1,2)*f[i-3]%mod;
f[i]+=C(i-1,3)*f[i-4]%mod;
}
}
void slove(int n)
{
cout<<f[n]%mod<<endl;
}
signed main()
{
int T;
cin>>T;
init(1e6);
//cout<<ifac[1000000]<<endl;
while(T--)
{
int x;
cin>>x;
slove(x);
}
return 0;
}
C
考虑预先处理出1e6内所有数约数。用约数筛处理
# include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+10;
int n;
vector<int> pri;
bool not_prime[N];
int d[N], num[N],s[N];
void pre(int n)
{
d[1] = 1;
for (int i = 2; i <= n; ++i)
{
if (!not_prime[i])
{
pri.push_back(i);
d[i] = 2;
num[i] = 1;
}
for (int pri_j : pri)
{
if (i * pri_j > n) break;
not_prime[i * pri_j] = true;
if (i % pri_j == 0)
{
num[i * pri_j] = num[i] + 1;
d[i * pri_j] = d[i] / num[i * pri_j] * (num[i * pri_j] + 1);
break;
}
num[i * pri_j] = 1;
d[i * pri_j] = d[i] * 2;
}
}
}
signed main()
{
pre(1e6+1);
for(int i=1;i<=1e6;i++)
{
s[i]=s[i-1]+(d[i]<=4);
}
int T;
cin>>T;
while(T--)
{
int l,r;
cin>>l>>r;
cout<<s[r]-s[l-1]<<endl;
}
}
B
发现反对称矩阵对称位置和为零,需要对称位置之和是偶数;
# include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
cin>>n;
vector a(n+1,vector<int>(n+1));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
}
}
bool st=1;
vector b(n + 1, vector<int>(n + 1));
vector c(n + 1, vector<int>(n + 1));
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
if(i!=j)
{
int val=a[i][j]+a[j][i];
if(val&1)
{
st=0;
cout<<"NO";
return 0;
}
else
{
int x = val / 2;
b[i][j] = b[j][i] = x;
int y = a[i][j] - x;
c[i][j] = y;
c[j][i] = -y;
}
}
else
{
b[i][j] = a[i][j];
}
}
}
if(st)
{
cout<<"YES"<<endl;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}
}
return 0;
}