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

来源:互联网 发布:边际效应 知乎 编辑:程序博客网 时间:2024/05/17 03:17

附上标准题解地址: 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
原创粉丝点击