2020hdoj multi-school competition fifth game 1001 1003 1009

Topic link 1001 1003 1009 The inverse element part of the code in
this article is borrowed from this blog. Link Problem-solving idea: The students who are playing together make it, post it here first, and then study it when you have time. The idea of ​​1001 is roughly the derivation of geometry , and then find the expectation, which is the sum of its reciprocal from 1 to n divided by n and then multiplied by 3. 1001AC code:


1/h^2=1/a^2+1/b^2+1/c^2

#include<iostream>
#include<cstdio>
using namespace std;
long long res[6000005];
long long mod=998244353;
void exgcd(long long a,long long b,long long &gcd,long long &x,long long &y)
{
    
    
    if(b==0)
    {
    
    
        x=1;
        y=0;
        gcd=a;
    }
    else
    {
    
    
        exgcd(b,a%b,gcd,y,x);
        y-=x*(a/b);
    }
}
long long inv(long long a,long long mod)
{
    
    
    long long x,y,gcd;
    exgcd(a,mod,gcd,x,y);
    return gcd==1?(x%mod+mod)%mod:-1;
}
int main()
{
    
    
    long long T,n;
    for(int i=1;i<=6000005;i++)
    {
    
    
        long long j=i;
        res[i]=inv(j*j,mod);
    }
    for(int i=2;i<=6000005;i++)
    {
    
    
        res[i]=(res[i]+res[i-1])%mod;
    }
    for(int i=1;i<=6000005;i++)
    {
    
    
        res[i]=3*res[i]*inv(i,mod)%mod;
    }
    scanf("%lld",&T);
    while(T--)
    {
    
    
        scanf("%lld",&n);

        printf("%lld\n",res[n]);
    }
    return 0;
}

Both 1003 and 1009 are looking for patterns. . . . . .

1003AC code:

#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
#include<stdlib.h>
using namespace std;
typedef long long ll;
long long mod=998244353;
struct number
{
    
    
    int d,x,y;
    bool operator <(const number q)
    {
    
    
        if(y==q.y)return x<q.x;
        return y<q.y;
    }
}p[1000005];
int main()
{
    
    
    int T,n,k;
    scanf("%d",&T);
    while(T--)
    {
    
    
        scanf("%d%d",&n,&k);
        for(int i=1;i<=2*n*(1<<k);i++)
        {
    
    
            scanf("%d",&p[i].d);
            p[i].x=1;
            p[i].y=i;
        }
        for(int i=1;i<=k;i++)
        {
    
    
            for(int j=1;j<=2*n*(1<<k);j++)
            {
    
    
                if(p[j].y<=2*n*(1<<k)/(1<<i))
                {
    
    
                    p[j].x=1+(1<<(i-1))-p[j].x;
                    p[j].y=1+2*n*(1<<k)/(1<<i)-p[j].y;
                }
                else
                {
    
    
                    p[j].x+=1<<(i-1);
                    p[j].y-=2*n*(1<<k)/(1<<i);
                }
            }
        }sort(p+1,p+1+2*n*(1<<k));
        for(int i=1;i<=2*n*(1<<k);i++)
        {
    
    
            if(i==1)printf("%d",p[i].d);
            else printf(" %d",p[i].d);
        }printf("\n");
    }
    return 0;
}

1009AC code:

#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
//long long res[6000005];
long long mod=998244353;
ll qpow(ll m,ll q)
{
    
    
    ll ans=1;
    while(q)
    {
    
    
        if(q&1)
            ans=ans*m%mod;
        m=m*m%mod;
        q>>=1;
    }
    return ans;
}
//ll inv[6000005];
ll getinv(ll n)
{
    
    
    ll num3=qpow(n,mod-2);
    return num3;
}

int main()
{
    
    
    long long T,n;
    scanf("%lld",&T);
    while(T--)
    {
    
    
        scanf("%lld",&n);
        ll res=(qpow(2,n)%mod+1%mod+(2%mod)*qpow((3*getinv(2))%mod,n)%mod)%mod;
        printf("%lld\n",res);
    }
    return 0;
}
[ Copy to Clipboard ]    [ Save to File]

Guess you like

Origin blog.csdn.net/weixin_44063734/article/details/107864509