《牛客2020年七夕节比赛》

A:百度永远滴神

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e5+5;
const int M = 1e4;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){  
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){
/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

int main()
{
    printf(
"D
"
"A
"
"ABCD
"
"D
"
"A
"
"C
"
"AB
"
"A
"
"B
"
"C
"
"B
"
"D
"
"D
"
"A
"
"ABC
"
"D
"
"B
"
"D
"
"D
"
"D
");
   // system("pause");
}
View Code

B:思维题。

当n >= 4时,n!!就会爆longlong。

因为模数最大到1e9,那么显然n!!再阶乘。

乘的数里面必定存在模数。然后模数对自己取模是0。那么最后的答案肯定是0。

所以在n >= 4时,答案必定是0.

n < 4时,暴力计算即可。

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e5+5;
const int M = 1e4;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){  
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){
/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

LL cal(LL x,LL m)
{
    LL ma = 1;
    for(rg int i = 1;i <= x;++i) ma = ma*i%m;
    return ma;
}
LL del(LL x)
{
    LL ma = 1;
    for(rg int i = 1;i <= x;++i) ma = ma*i;
    return ma;
}
int main()
{
    int ca;ca = read();
    while(ca--)
    {
        int n,m;n = read(),m = read();
        if(n >= 4) printf("0
");
        else printf("%lld
",cal(del(del(n)),m));
    }
  //  system("pause");
}
View Code

C:找下画法就行

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 1e5+5;
const int M = 1e4;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){  
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){
/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

int main()
{
    int n;n = read();
    printf("I love U forever.
");
    int m = n,sp = n;
    for(rg int i = 1;i <= n;++i)
    {
        for(rg int j = 1;j <= sp;++j) printf(" ");
        for(rg int j = 1;j <= m;++j) printf("*");
        for(rg int j = 1;j <= 2*sp-1;++j) printf(" ");
        for(rg int j = 1;j <= m;++j) printf("*");
        printf("
");
        sp--,m += 2;
    }
    m -= 2;
    m = m*2+3;
    for(rg int i = 1;i <= m;++i) printf("*");
    printf("
");

    while(1)
    {
        sp++,m -= 2;
        if(m <= 0) break;
        for(rg int j = 1;j <= sp;++j) printf(" ");
        for(rg int j = 1;j <= m;++j) printf("*");
        printf("
");
    }
   // system("pause");     
    return 0;
}
View Code

D:数论。

第一眼在思考dp。

那么如果这个题可以重复,我们可以将所有因子都看成一个单独的数。

对原来的数质因数分解后,可以dp找到组成积i的最小和代价。

但是这个地方不能出现重复的因子。那么问题就变得麻烦了。

考虑直接去搜:我们边搜边分解因子,然后去更新答案

具体搜就是要把原来的数在质因子分解中的每种方案都算一下最小值。

注意剪枝和重复的判断。

因为这里最少因子个数要>1.那么ans一开始设为1+n即1*n。

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e5+5;
const int M = 1e4;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){  
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){
/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

LL ans;
void dfs(int num,LL sum,LL x)
{
    if(num*num > x) return ;//因子过半,返回
    if(x%num == 0)
    {
        if(num%(x/num) == 0 && num == x/num);  
        else
        {
            ans = min(ans,sum+x/num+num);
            dfs(num+1,sum+num,x/num);
        }
    }
    dfs(num+1,sum,x);
}
int main()
{
    int n;n = read();
    ans = n+1;//1*n
    dfs(2,0,n);
    printf("%lld
",ans);
   // system("pause");
}
View Code

E:LCA搞搞就行,考虑到询问过多,就用了ST表

// Author: levil
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e5+5;
const int M = 1e4;
const LL Mod = 1e9+7;
#define rg register
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline LL read(){  
        LL x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-') f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x<<1)+(x<<3)+(c^48);c = getchar();}
        return x*f;
    }
    void print(int x){
        if(x < 0){x = -x;putchar('-');}
        if(x > 9) print(x/10);
        putchar(x%10+'0');
    }
}
using namespace FASTIO;
void FRE(){
/*freopen("data1.in","r",stdin);
freopen("data1.out","w",stdout);*/}

int lg[N<<1],head[N],f[N<<1][20],dfn[N],dep[N];
int n,q,cnt = 0,tot = 0;
struct Node{int to,next;}e[N<<1];
inline void add(int u,int v)
{
    e[++cnt].to = v,e[cnt].next = head[u],head[u] = cnt;
}
void dfs(int u,int fa)
{
    dep[u] = dep[fa]+1;
    f[++tot][0] = u;
    dfn[u] = tot;
    for(rg int i = head[u];i;i = e[i].next)
    {
        if(e[i].to == fa) continue;
        dfs(e[i].to,u);
        f[++tot][0] = u;
    }
}
void init()
{
    lg[1] = 0;for(rg int i = 2;i <= tot;++i) lg[i] = lg[i>>1]+1;
    for(rg int j = 1;j <= 19;++j)
    {
        for(rg int i = 1;i+(1<<j)-1 <= tot;++i)
        {
            if(dep[f[i][j-1]] <= dep[f[i+(1<<j-1)][j-1]]) f[i][j] = f[i][j-1];
            else f[i][j] = f[i+(1<<j-1)][j-1];
        }
    }
}
int LCA(int x,int y)
{
    x = dfn[x],y = dfn[y];
    if(x > y) swap(x,y);
    int k = lg[y-x+1];
    if(dep[f[x][k]] < dep[f[y-(1<<k)+1][k]]) return f[x][k];
    return f[y-(1<<k)+1][k];
}
int dis(int x,int y)
{
    return dep[x]+dep[y]-2*dep[LCA(x,y)];
}
LL quick_mi(LL a,LL b)
{
    LL re = 1;
    while(b)
    {
        if(b&1) re = re*a%Mod;
        a = a*a%Mod;
        b >>= 1;
    }
    return re;
}
int main()
{
    n = read();
    for(rg int i = 1;i < n;++i)
    {
        int u,v;u = read(),v = read();
        add(u,v);add(v,u);
    }
    dfs(1,0);
    init();
    q = read();
    LL ans = 0;
    for(rg int i = 1;i <= q;++i)
    {
        int a,b,c,t;
        a = read(),b = read(),c = read(),t = read();
        int pt = dis(c,b)+dis(b,1);
        int nn = dis(a,1);
        if(nn >= t || pt >= t) continue;
        ans++;
    }
    LL ma = ans*quick_mi(q,Mod-2)%Mod;
    printf("%lld
",ma);
   // system("pause");
}
View Code
原文地址:https://www.cnblogs.com/zwjzwj/p/13563305.html