noj--1624 cul-de-sac (simulation)

Source: Internet
Author: User

  • [1624] dead End
  • Time limit: Ms Memory limit: 65535 K
  • Problem description
  • A dead end consists of a row of n squares, numbered from 1 to N.
    The Laboratory's "pig" began in the 1th lattice, began to move forward, each step of a box, and every walk of K will mark the current grid (at the beginning, the number 1th lattice also has a mark). Since this is a dead end, whenever "pig" goes to the leftmost or most right lattice, it will change direction. The curious "pig" thought: if I keep walking, can I mark all the squares?

    Smart you must know the answer!

    Hint1: If n=6,k=2, the position change is: 1, 3, 5, 5, 3, 1, 3, 5 ..... Obviously, it is not possible to mark all the squares at this time. such as

  • Input
  • Multiple sets of input data (number of groups <=100)
    A row of data for each group containing two positive integers n and K.
    (1 <= N <= 100000, 1 <= k <= 100000)
  • Output
  • For each set of data output a row of YES or NO indicates whether all the squares can be labeled.
  • Sample input
  • 6 26 3
  • Sample output
  • NOYES
  • Tips
  • No
  • Source
  • 2015 Suzhou University ACM-ICPC Training Team Tryouts (1)

This problem was done during the school period, at that time compared to naive is also want to simulate the idea is the head of the reverse array reversed to go, and then the wit to tle. Suddenly want to come back to do this problem, of course, or simulation, the idea is to continue to go beyond the range, and then the current POS symmetrical back, a kind of folding feeling ... It turns out that the method is feasible, but how can the circulating volume of a tle,10w be tle? And then find a couple of days. Data: 4 99999, similar to this k very large n very small case. While the while inside the loop huge number of times, when the estimated data amount of tens of millions or even higher then what to do, open a structure array record this point left and right to go through this point. Assuming that the number of points to the left or to the right is greater than or equal to 2, then the point in the corresponding direction is passed at least two times, it can be judged that this group of data is not out of the way-only the array will not go out at some point repeated walk. Otherwise let it cycle through.

Finally worship the 0ms code length is only 300 +. Estimation is a mathematical method, we have not enough IQ and lazy ... Force simulation.

Code:

#include <iostream> #include <algorithm> #include <cstdlib> #include <sstream> #include < cstring> #include <cstdio> #include <string> #include <deque> #include <stack> #include < cmath> #include <queue> #include <set> #include <map>using namespace std;typedef long Long ll;# Define INF 0x3f3f3f3fstruct info{int a;int b;}; Info pos[100010];int Main (void) {int n,k,i,res;while (~scanf ("%d%d", &n,&k)) {memset (pos,0,sizeof (POS)); if (k= =1| | N==1)//Special award {puts ("yes"); continue;} int moni=0;//Analog Loop volume bool Flag=0;int chushi=1;/initial position int cheng=1;//left/right int cnt=0;//walk through the number of roads (not repeating) while (moni<=100010) { Chushi+=cheng*k;while (chushi>n| | chushi<1) {if (chushi>n) {cheng=-1;chushi=n-(chushi-n);} if (chushi<1) {Cheng=1;chushi=2-chushi;}} if (pos[chushi].a>=2| | pos[chushi].b>=2)//Repeat through, mark after Break{flag=0;break;} if (cheng==1)//continue to walk {if (pos[chushi].a==0) {cnt++;} pos[chushi].a++;} else//go back {if (pos[chushi].b==0) {cnt++;} pos[chushi].b++;} if (cnt==n)//analog Arrival, BREAK{FLAG=1;BREak;} moni++;} if (flag) {puts ("YES");} Else{puts ("NO");}} return 0;}

  

noj--1624 cul-de-sac (simulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.