B - Vile Grasshoppers (codeforces)

本文探讨了一种策略博弈问题,目标是在一棵有编号的松树上找到最高且安全的树枝,避免被跳跃能力惊人的蚱蜢干扰。通过算法分析,文章提供了寻找符合条件的树枝的解决方案。

The weather is fine today and hence it’s high time to climb the nearby pine and enjoy the landscape.

The pine’s trunk includes several branches, located one above another and numbered from 2 to y. Some of them (more precise, from 2 to p) are occupied by tiny vile grasshoppers which you’re at war with. These grasshoppers are known for their awesome jumping skills: the grasshopper at branch x can jump to branches .

Keeping this in mind, you wisely decided to choose such a branch that none of the grasshoppers could interrupt you. At the same time you wanna settle as high as possible since the view from up there is simply breathtaking.

In other words, your goal is to find the highest branch that cannot be reached by any of the grasshoppers or report that it’s impossible.

Input
The only line contains two integers p and y (2 ≤ p ≤ y ≤ 109).

Output
Output the number of the highest suitable branch. If there are none, print -1 instead.

Examples
Input
3 6
Output
5
Input
3 4
Output
-1
Note
In the first sample case grasshopper from branch 2 reaches branches 2, 4 and 6 while branch 3 is initially settled by another grasshopper. Therefore the answer is 5.
It immediately follows that there are no valid branches in second sample case.
题意:从2 到 y 的区间找出一个最大的数,满足不能2到p的数整除。
思路:从区间(p+1,y)区间从后向前找,若为合数,则判断他的最小因子是否大于p,如果大于p则满足,反之若是素数也满足。

#include<iostream>
 #include<cstring>
  #include<string>
   #include<cstdio>
    #include<cmath>
    using namespace std;
     typedef long long ll;
int j(ll a, ll k)
{
    ll sqr = (ll)sqrt((double)a);
     ll i;
      for(i = 2 ; i <= sqr ; ++ i)
         if( a % i == 0 ) break;
        if((i > k && a / i > k) || i > sqr) return 1;
      return 0;
}
int j2(ll a,ll k)
{
     for(int i = 2 ; i <= k ; ++ i)
        if(a % i == 0) return 0;
          return 1;
}
  int main()
  {

      ll a , b;
      cin >> a >> b;
      if(a <= 10)
          {
            while(b > a)
               {
                  if(j2(b,a))
                  {
                    printf("%lld\n",b);
                    return 0;
                  }
                  b--;
               }
            puts("-1");
          }
        else
        {
             while ( b > a )
             {
                 if (j(b,a) )
                   {
                     printf("%lld\n",b);
                     return 0;
                   }
                b--;
             }
            return puts("-1");
        }
      return 0;
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值