南京理工大学第八届程序设计大赛题解

附上标准题解地址: https://icpc.njust.edu.cn/gist/comzyh/a36db955-1113-4fa2-9000-2beb0bdc7201/

A:偷吃糖果

<水题> 注意看清为连续字符!!!


B:Banana


C:count_prime

先对n分解质因数,分别记录下每个质因数。求[a,b]中与n互质的数的个数,可以转换成求[1,b]中与n互质的数个数减去[1,a-1]与n互质的数的个数。根据容斥定理求出[1,x]中与n互质的数的个数。、

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
int T,cas;
ll a,b,n;
vector<ll> adj;

ll solve(ll x,ll n)
{
    adj.clear();
    ll i,j;
    for (i=2;i*i<=n;i++)
    {     
        if (n%i==0)
        {
            adj.push_back(i);
            while (n%i==0)
                n/=i;
        }
    }
    if (n>1)  adj.push_back(n);

    ll sum=0,value,cnt;
    for (i=1;i<(1<<adj.size());i++)  
    {
        value=1;
        cnt=0;
        for (j=0;j<adj.size();j++)
            if (i&(1<<j))      
            {
                value*=adj[j];
                cnt++;
            }
        if (cnt&1)      
            sum+=x/value;
        else
            sum-=x/value;
    }
    return x-sum;
}

int main()
{
    //cas=0;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%lld%lld%lld",&a,&b,&n);
        printf("%lld\n",solve(b,n)-solve(a-1,n));
    }
    return 0;
}

D:triple


E:sad


F:sequence

经典贪心问题,从前往后扫描原数组,每到一个数(若没遍历过),就从它开始往后组一个不下降子序列,每次组完,计数+1,直到数组被取空。

AC代码:

#include <iostream>
#include <cstdio>
using namespace std;
int height[10000];
int main()
{
    int T;cin>>T;
    while(T--)
    {
        int n;cin>>n;
        int number;
        int index = 0;
        while(n--)
        {
            scanf("%d",&number);
            int tmp_index = -1;
            for (int i = 0; i < index; ++i)
            {
                if(number>=height[i] && (tmp_index == -1 || height[tmp_index] < height[i]))
                {
                    tmp_index = i;
                }
            }
            if(tmp_index != -1)
            {
                height[tmp_index] = number;
            }
            else height[index++] = number; 
        }
        cout<<index<<endl;
    }
}

G:琪露诺的算术教室


H:谁才是最强战队


I:puzzle


J:water1

结果为 (h+1)*sum(wi) - 各海拔,

AC代码:

#include <iostream>
#include <string>
#include <string.h>
#include <cstdio>

#define LL long long 
using namespace std;
const int maxn = 1e5+5;

int n;
int h[maxn],w[maxn];

int main()
{
    while ( scanf("%d",&n)!=EOF )
    {
        if ( n==0 )
        {
            puts("1");
            continue;
        }
        int maxh = -1;
        {
            scanf("%d%d", &h[i], &w[i]);
            maxh = max(maxh, h[i]);
        }
        LL ans = 0;
        for(int i=1;i<=n;i++)
            ans += (LL)w[i] * (maxh+1 - h[i]);
        printf("%lld\n", ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值