[CSP-S simulation tests 67] explanations

Many years later, I can finally there has been a complete change of title ......

A. God Yan Wong

Met this requirement is clearly divisible title split out gcd

设$d=gcd(a,b)\ \ \ a'=\frac{a}{d} \ \ \ b'=\frac{b}{d}$

Original style into $ (a'd + b'd) | (a'db'd) $

$(a'+b')|(a'b'd)$

And because $ gcd (a ', b') = 1 $

Therefore $ a '+ b' $ certainly not $ a'b '$ factor, then get $ (a' + b ') | d $

又由$a+b \leq n \rightarrow (a'+b')d \leq n \rightarrow a'+b' \leq \sqrt{n}$

Might enumeration $ s = a '+ b' $

So to meet the $ gcd (a ', b') = 1 $ lawful number of how many?

Decreases by the technique available $ gcd (a, b) = gcd (a + b, b) $

Therefore $ a ', b' $ Total $ \ varphi (s) $ of

$ D $ was legitimate, it was only $ \ frac {n} {s ^ 2} $ two

Answer at $ \ sum \ limits _ {i = 2} ^ {n} \ varphi (i) \ times \ frac {n} {i ^ 2} $

Note that when $ i> \ sqrt {n} $ when each is 0, enumerates $ \ sqrt {n} $ to

Complexity $ O (\ sqrt {n}) $.

 

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1e7+2;
ll n,ans,sqt;
int pr[N],vis[N],tot,phi[N];
void work()


{
	phi[1]=1;
	for(int i=2;i<=sqt;i++)
	{
		if(!vis[i])pr[++tot]=i,phi[i]=i-1;
		for(int j=1;j<=tot&&pr[j]*i<=sqt;j++)
		{
			vis[i*pr[j]]=1;
			if(i%pr[j])phi[i*pr[j]]=phi[i]*(pr[j]-1);
			else
			{
				phi[i*pr[j]]=phi[i]*pr[j];
				break;
			}
		}
		ans+=1LL*phi[i]*(n/(1LL*i*i));//cout<<ans<<endl;
	}
}
int main()
{
	scanf("%lld",&n);
	sqt=sqrt(n);
	work();
	/*for(int i=1;i<=n;i++)
		cout<<i<<' '<<phi[i]<<endl;*/
	cout<<ans<<endl;
	return 0;
}

 

 

 

 

B. Huang Lei drop

LIS is coupled ...... Numbers by weighting schemes can process both the information segment tree only once, each time the longest query sequence length and the number of common sub-programs corresponding to the current sequence value can be transferred. Note intervals query about border issues.

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#define pa pair<int,int>
using namespace std;
const int N=1e5+5;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
typedef long long ll;
const ll mod=123456789;
int n,a[N],type,side;
int ans,f[N<<2],res=1;
ll num,g[N<<2];
#define ls(k) (k)<<1
#define rs(k) (k)<<1|1
void up(int k)
{
    f[k]=max(f[ls(k)],f[rs(k)]);
    if(f[ls(k)]>f[rs(k)])g[k]=g[ls(k)];
    else if(f[rs(k)]>f[ls(k)])g[k]=g[rs(k)];
    else g[k]=(g[ls(k)]+g[rs(k)])%mod;
}
void ins(int k,int l,int r,int pos,int valf,ll valg)
{
    if(l==r)
    {
        if(f[k]<valf)f[k]=valf,g[k]=valg;
        else if(f[k]==valf)(g[k]+=valg)%=mod;
        return ;
    }
    int mid=l+r>>1;
    if(pos<=mid)ins(ls(k),l,mid,pos,valf,valg);
    else ins(rs(k),mid+1,r,pos,valf,valg);
    up(k);
}
void ask(int k,int l,int r,int L,int R)
{
    if(L>R)return ;
    if(L<=l&&R>=r)
    {
        if(ans<f[k])ans=f[k],Surely g [k];
        else if(ans==f[k])(num+=g[k])%=mod;
        return ;
    }
    int mid=l+r>>1;
    if(L<=mid)ask(ls(k),l,mid,L,R);
    if(R>mid)ask(rs(k),mid+1,r,L,R);
}

int main()
{
    n=read();type=read();
    for(int i=1;i<=n;i++)
        a[i]=read(),side=max(side,a[i]);
    int nowf=1;ll nowg=1;
    ins(1,1,side,a[1],nowf,nowg);
    for(int i=2;i<=n;i++)
    {
        ans=num=0;
        ask(1,1,side,1,a[i]-1);
        nowf=ans+1;nowg=max(1LL,num);
        //cout<<nowf<<' '<<nowg<<endl;
        res=max(res,nowf);
        ins(1,1,side,a[i],nowf,nowg);
    }
    printf("%d\n",res);
    if(!type)return 0;
    ans=num=0;
    ask(1,1,side,1,side);
    printf("%lld\n",num);
    return 0;
}

 

Genma C. Wong

Like the idea of ​​God

The first step to kneel ...... never thought to enumerate distance

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5005;
const ll mod=123456789;
ll f[N],sum[N],ans[N<<1];
int n;
int main()
{
    scanf("%d",&n);
    f[1]=f[3]=1;sum[1]=sum[2]=1;sum[3]=2;
    for(int i=4;i<=n;i++)
        f[i]=(f[i-1]+f[i-2])%mod,sum[i]=(sum[i-1]+f[i])%mod;
    for(int i=1;i<n;i++)
        (ans[i]+=sum[n-i]*f[i+1]%mod)%=mod;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            int d1=i+j,d2=max(i,j);
            (ans[d1]+=(sum[n-d2+1]-1)*f[j+1]%mod*f[i]%mod)%=mod;
        }
    for(int i=1;i<=n*2;i++)
        printf("%lld ",ans[i]);
    putchar('\n');
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Rorschach-XR/p/11648624.html