zoukankan      html  css  js  c++  java
  • codeforces #div2.462 C.Cave Painting

    C. Cave Painting
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Imp is watching a documentary about cave painting.

    Some numbers, carved in chaotic order, immediately attracted his attention. Imp rapidly proposed a guess that they are the remainders of division of a number n by all integers i from 1 to k. Unfortunately, there are too many integers to analyze for Imp.

    Imp wants you to check whether all these remainders are distinct. Formally, he wants to check, if all 1 ≤ i ≤ k, are distinct, i. e. there is no such pair (i, j) that:

    • 1 ≤ i < j ≤ k,
    • , where  is the remainder of division x by y.
    Input

    The only line contains two integers nk (1 ≤ n, k ≤ 1018).

    Output

    Print "Yes", if all the remainders are distinct, and "No" otherwise.

    You can print each letter in arbitrary case (lower or upper).

    Examples
    input
    4 4
    output
    No
    input
    5 3
    output
    Yes
    Note

    In the first sample remainders modulo 1 and 4 coincide.

     题意:

    给你一个n,k,让你判断n对1到k的所有数字取余结果是否有相同的,若有,输出Yes

    分析:

    一开始看到这个题,想的就是,暴力模拟,然而卡在了数据范围太大,按照我过去的经验,我可能会傻乎乎的(好吧,其实我不想说自己傻....)去想办法解决开不了这么大数组的问题,然而此时,我应该想的是换一种方法来做,正面走不通走反面嘛,分析一下要是保证每一个余数都不同需要满足什么样的条件呢?(不知道我下一次碰到类似的情况能不能这样来思考.....)

    我们可以注意到,n对i取余的结果有i种情况,即从0到i-1,那么,n%1=0,n%2就只能是1(要保证每一个余数都不同的话)

    所以对于每一个i,n%i==i-1,就可以保证余数都不同了。

    所以问题就转换为:判断每一个i,n%i是否等于i-1

    代码如下(注意数据范围  开long long ):

     1 #include<stdio.h>
     2 long long n,k;
     3 int main()
     4 {
     5 
     6     while(~scanf("%I64d %I64d",&n,&k))
     7     {
     8         long long i;
     9         for(i=1;i<=k;i++)
    10         {
    11             if(n%i!=i-1)
    12             {
    13                 printf("No
    ");
    14                 break;
    15             }
    16         }
    17         if(i==k+1)
    18             printf("Yes
    ");
    19     }
    20 
    21 
    22 
    23 
    24 
    25     return 0;
    26 }

    所以,要学会反向思考问题,之前想的是判断每一个余数是否相同,在这种复杂度很高无法实现的情况下,就应该想到换条思路,从别的角度来想这个问题,比如,要使结果为yes,那应该满足什么条件呢?

    PS:说起来,很多问题的解决往往就在于换个思路,角度去看,不管是做题还是生活都是这样,看来道理都是相通的,就是不知道自己下次遇到类似问题能不能想到换个思路........

  • 相关阅读:
    解读基本数据类型和内置方法(1)(要掌握)
    简单循环流程的介绍
    基本数据类型的使用和运算符的介绍
    开辟编程语言的介绍和变量
    HTML5 元素超出部分滚动, 并隐藏滚动条
    数据库多行数据合并一行(sqlserver、Oracle、Mysql)
    Js apply方法与call方法详解 附ES6新写法
    Java实现牛顿迭代法求解平方根、立方根
    为什么在JavaScript中0.1+0.2不等于0.3?
    html5手机web页面底部菜单
  • 原文地址:https://www.cnblogs.com/renxin123/p/8433263.html
Copyright © 2011-2022 走看看