2020-2-7赛

感觉今天模拟题特别多。。。。。

*做过类似的题,但是它的数据限制条件太多。。。一定不能想当然*

问题 A: NH2012数列

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

给定一个等差数列,第一项是a, 从第二项开始,每项与前一项的差都是一个定值b。如果用数学形式来表示,那么可以表示成 a + b × x , 其中 x≧0,且是整数。例如: a = 1, b=2, 那么这个等差数列就是:1,3,5,7,9…
再给定一个等比数列,第一项是c, 从第二项开始,每项是前一项的d倍。如果用数学形式来表示等比数列,则是 c ×(dy)。 其中 y≧0, 且是整数。例如: c = 2, d = 3, 那么这个等比数列就是:2,6,18,54…
你的任务是计算在1至upperBound内的正整数,有多少正整数是“合法”的?
所谓的“合法”是指:该整数属于上面给定的等差数列的某项或者属于等比数列的某项,或者既属于等差数列的项也属于等比数列的项。

 

输入

一行,5个整数,分别是a,b,c,d,upperBound。
(1≤a,b,c,upperBound≤1012,  1≤d≤105。)
对于80%的数据,1≤upperBound≤1000000。

 

输出

一个整数,表示“合法”正整数的个数。

样例输入 Copy

【样例1】
1  1  1  2  1000
【样例2】
3  3  1  2  1000
【样例3】
452  24  4  5  600

样例输出 Copy

【样例1】
1000
【样例2】
343
【样例3】
10

提示

样例1解释:产生的等差数列是:1,2,3,4,….产生的等比数列是:1,2,4,8,….所以【1,1000】范围内所有正整数都是“合法”的。

样例3解释:“合法”的10个数分别是: 4,20,100,452,476,500,524,548,572,596

解题思路:分别找出等差cnt1,和等比cnt2,由于等比肯定比等差更少(通常,趋势),所以我们在cnt2里面去重

错的/
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a,b,c,d,up;
    scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&up);
    long long int cnt1=(up-a)/b+1;
    long long int cnt2=0;
    if(d==1)
    {
        if((c%b)==(a%b)&&c>=a)
            cnt2=0;
        else
            cnt2=1;
    }

    else
    {
        while(c<=up)
        {

        if((c>=a)&&((c%b)==(a%b)))
        {
            c*=d;
        }

        else if(c<=up)
        {
            cnt2++;
            c*=d;
        }
    }

}
  //printf("cnt1=%lld,cnt2=%lld\n",cnt1,cnt2);
    printf("%lld",cnt1+cnt2);

    return 0;
}

 

///正确//
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a,b,c,d,up,cnt1=0;
    scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&up);
    if(up>=a)
    {
        cnt1=(up-a)/b+1;
    }
    long long int cnt2=0;
    if(d==1)
    {
        if( ( ((c-a)%b)!=0&&(c<=up)) ||((c<a)&&(c<=up)))
            cnt2++;
    }
    else
    {
        while(c<=up)
        {
            if( (((c-a)%b!=0)&&(c<=up))|| ((c<a)&&(c<=up)))
                cnt2++;
            c*=d;
        }

    }
// printf("cnt1=%lld,cnt2=%lld\n",cnt1,cnt2);

    printf("%lld",cnt1+cnt2);

    return 0;
}

 

题意理解很关键

问题 O: Milk Factory

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

The milk business is booming! Farmer John's milk processing factory consists of N processing stations, conveniently numbered 1…N (1≤N≤100), and N−1 walkways, each connecting some pair of stations. (Walkways are expensive, so Farmer John has elected to use the minimum number of walkways so that one can eventually reach any station starting from any other station).
To try and improve efficiency, Farmer John installs a conveyor belt in each of its walkways. Unfortunately, he realizes too late that each conveyor belt only moves one way, so now travel along each walkway is only possible in a single direction! Now, it is no longer the case that one can travel from any station to any other station.

However, Farmer John thinks that all may not be lost, so long as there is at least one station i such that one can eventually travel to station i from every other station. Note that traveling to station i from another arbitrary station j may involve traveling through intermediate stations between i and j. Please help Farmer John figure out if such a station i exists.

输入

The first line contains an integer N, the number of processing stations. Each of the next N−1 lines contains two space-separated integers ai and bi with 1≤ai,bi≤N and ai≠bi. This indicates that there is a conveyor belt that moves from station ai to station bi, allowing travel only in the direction from ai to bi.

输出

If there exists a station i such that one can walk to station i from any other station, then output the minimal such i. Otherwise, output −1.

样例输入 Copy

3
1 2
3 2

样例输出 Copy

2

 

解析:这道题就是看这堆start——>end中有无一个点(样例中的2)能够通往其他点(1、3),注意这道题的数据,n个站台,但是只有n-1个通道,本来是n-1个<——>双向通道的,但是安装错了,然后你看,1<—>2变成1—>2 ,2<—>3变成3—>2; 那么

如果存在两种及以上类似于此的安装,则-1(4<—>5变成4—>5 ,5<—>6变成6—>5 ,那么3和5永远通不了,所以不存在任意j到i) 

开始以为要用链表,把能够通的首尾勾连起来,但是并不需要,我可以统计A[start]的个数,如果存在A【2】==0,并且只有它为0,说明

 

#include<bits/stdc++.h>
using namespace std;
int A[105];
int main()
{
  int n,a,b;
  scanf("%d",&n);
  for(int i=1;i<n;i++)
  {
      scanf("%d %d",&a,&b);
      A[a]++;//a,b可能有重复
  }
  int ans=-1,f=0;
  for(int i=1;i<=n;i++)
  {
      if(A[i]==0)
      {
          f++;
          ans=i;
      }
  }
  if(f>1) ans=-1;
  printf("%d",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值