[HDU]week5

题目地址

7131.Nun Heh Heh Aaaaaaaaaaa

#include <iostream>
#include <cstring>

using namespace std;

typedef long long ll;
const ll mod = 998244353;

int dp[100005][10];

ll mod_pow(ll n) //使用快速幂运算求2^n % mod
{
    ll ans = 1, a = (ll)2;
    while (n){
        if (n & 1) {
            ans = ans * a % mod;
        }
        a = a * a % mod;
        n >>= 1;
    }
    return ans % mod;
}

ll ksm(ll base, ll power)//快速幂求base^power % mod
{
	ll ans = 1;
	while(power)
	{
		if(power&1)
			ans = ans*base%mod;
		power = power>>1;
		base = base*base%mod;
	}
	return ans;
}

int main(){
    int T;
    cin >> T;
    while (T--){
        string s, t = "nunhehheh";
        memset(dp, 0, sizeof(dp));
        cin >> s;
        int n = s.length(), m = t.length();
        s = " " + s, t = " " + t;
        for (int i = 0; i <= n; i++) {
            dp[i][0] = 1;
        }
        int num_a = 0;
        for (int i = 1; i <= n; i++){
            for (int j = 1; j <= m; j++){
                if (j > i) {
                    continue;
                }
                if (s[i] == t[j]) {
                    dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j]) % mod;
                }else {
                    dp[i][j] = dp[i - 1][j] % mod;
                }
            }
            if(s[i] == 'a')
				num_a++;
        }
        ll res = 0;
        for (int i = 1; i < n; i++){
            if (s[i] == 'a')
                num_a--;
            res = (res+((dp[i][m] - dp[i-1][m] + mod) % mod
                         * (ksm(2,num_a)-1))) % mod;
        }
        cout << res << endl;
    }
    return 0;
}

run:
成功截图

7129.Primality Test

中文参考
题意 :f(x)是严格大于x的最小质数,g(x)=[f(x)+f(f(x))]/2的向下取整,判断g(x)是否是质数
思路 :f(f(x))即f(x)相邻的下一个质数,g(x)在相邻两个质数之间,所以g(x)一定是合数,除了x=1的情况下,f(1)=2,f(2)=3,即除了2和3之间以外
语法 :long long 9e18多,int 2e9多

#include <iostream>
#include <cstdio>
using namespace std;
 
int main()
{
    int T;
    cin >> T;
    while (T --)
    {
        long long x;
        scanf("%lld", &x);
        if (x == 1){
            puts("YES");
        } else {
            puts("NO");
        }
    }
    return 0;
}

When x=1, f(x)=2, f(f(x))=f(2)=3, then g(x)=⌊2+32⌋=2, which is a prime. So the output is 𝚈𝙴𝚂.
When x=2, f(x)=3, f(f(x))=f(3)=5, then g(x)=⌊3+52⌋=4, which is not a prime. So the output is 𝙽𝙾.

7127.Kanade Doesn’t Want to Learn CG

#include <iostream>
#define endl '\n'

using namespace std;

typedef long long ll;

ll a, b, c, x0, x1, y0, y1, y2;

ll f(ll x)
{
    return a * x * x + b * x + c;
}

int main()
{
    int T;
    cin >> T;
    
    while (T -- )
    {
        cin >> a >> b >> c >> x0 >> x1 >> y0 >> y1 >> y2;
        if (f(x0) <= y0) cout << "No" << endl;
        else if (f(x1) > y2) cout << "No" << endl;
        else if (f(x1) == y0) cout << "No" << endl;
        else if (f(x1) >= y0 && f(2 * x1 - x0) >= y0) cout << "No" << endl;
        else cout << "Yes" << endl;
    }
    
    return 0;
}

7136.Jumping Monkey

参考

#include<bits/stdc++.h>
using namespace std;
int T,n;//节点数n
int head[100005],net[300005],v[300005],d[100005],w[100006],vis[100005];//原图,其中w为权值,d为深度
int tot=0,tot2;//原图边的数目tot1,新的连通图变得个数tot2
int fa[100005];
int head2[100005],net2[300005],v2[300005];//新的连通图
void add(int x,int y)//构造原图
{
    v[++tot]=y,net[tot]=head[x],head[x]=tot;
}
void add2(int x,int y)//构造新的连通图
{
    v2[++tot2]=y,net2[tot2]=head2[x],head2[x]=tot2;
}
void init()//初始化
{
    tot=0;
    tot2=0;
    for(int i=1;i<=n;i++)
    {
        d[i]=0,head[i]=0,vis[i]=0,head2[i]=0,fa[i]=i,vis[i]=0;
    }
}
int find(int x)//并查集查询节点x对应连通块中权值最大的点
{
    if(x==fa[x])
    return x;
    else
    {
        return fa[x]=find(fa[x]);
    }
}

struct node
{
    int p,w;//p为节点编号,w为节点权值
    const bool operator<(node y)const//按照权值从小到大排序
    {
        return this->w<y.w;
    }
}que[100005];

void bfs(int x)
{
    vis[x]=1;
    for(int i=head[x];i;i=net[i])//在原图中找到x点一步能到达的点
    {
        int y=v[i];
        if(!vis[y])//这个点不在新构造的连通图中
        continue;
        int ty=find(y);//查询这个节点对应连通块中权值最大的点

        if(ty==x)//如果这个连通块中权值最大的点不是x就令fa[ty]=x,并且在x和ty之间添加有向边e(x->ty)
        continue;
        add2(x,ty);
        fa[ty]=x;
    }
}
void bfs2(int x)//以x节点为根节点在新构造的连通图中求每个节点的深度
{
    queue<int>q;
    q.push(x);
    d[x]=1;//根节点深度为1
    while(q.size())
    {
        x=q.front();
        q.pop();
        for(int i=head2[x];i;i=net2[i])
        {
            int y=v2[i];
            if(d[y])
            continue;
            d[y]=d[x]+1;
            q.push(y);
        }
    }
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        init();
        for(int i=1;i<n;i++)//输入原图
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        for(int i=1;i<=n;i++)//输入每个点的权值
        {
            scanf("%d",&w[i]);
            que[i].p=i;
            que[i].w=w[i];
        }
        sort(que+1,que+1+n);//对每个点按照权值大小从小到大排序

        for(int i=1;i<=n;i++)//排序后从小到大将每个节点加入新构造的连通图
        {
            bfs(que[i].p);
        }

        bfs2(que[n].p);//以权值最大的节点为根节点求每个节点的深度
        for(int i=1;i<=n;i++)//输出每个节点的深度,也就是解
        printf("%d\n",d[i]);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值