其他分享
首页 > 其他分享> > CF 722 DIV2 D.Kavi on Pairing Duty(线性筛+递推)

CF 722 DIV2 D.Kavi on Pairing Duty(线性筛+递推)

作者:互联网

a[i]=sigma a[j]  +fac[i]

j从1到i-1

fac:因子个数,比如4有1,2,4三个因子,如果有质因数p1,p2,p3,对应指数为a1,a2,a3,则fac[i]=(a1+1)*(a2+1)*(a3+1)

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define mod 998244353
#define Maxn 1000009
using namespace std;
typedef long long ll;
ll a[999999],n,sum[Maxn];
int prime[Maxn];
int fac[Maxn],cnt[Maxn],cntt;
void ini()
{
	for(int i=2;i<=Maxn;i++)
	{
		if(!prime[i])
		{
			prime[cntt++]=i;
			fac[i]=2;//factor
			cnt[i]=1;//
		}
		for(int j=0;j<cntt;j++){
			if(prime[j]*i>Maxn)break;
			prime[prime[j]*i]=1;
			if(i%prime[j]==0)
			{
				fac[i*prime[j]]=fac[i]/(cnt[i]+1)*(cnt[i]+2);
				cnt[i*prime[j]]=cnt[i]+1;
				break;
			}
			else
			{
				fac[i*prime[j]]=fac[i]*fac[prime[j]];
				cnt[i*prime[j]]=1;
			}
		}
	}
	return;
}
int i,j,k=1;
int main()
{
	scanf("%lld",&n);
	a[1]=1;
	a[2]=3;
	a[3]=6;
	sum[1]=1;
	sum[2]=4;
	sum[3]=10;
	ini();
	for(int i=4;i<=n;i++)
	{
		a[i]+=sum[i-1]+fac[i]%mod;
		sum[i]=sum[i-1]+a[i]%mod;
	}
	printf("%lld",a[n]%mod);
	return 0;
}

 

标签:Duty,prime,cnt,int,sum,Pairing,CF,fac,include
来源: https://blog.csdn.net/DovahkiinGA/article/details/117289277