2018ACM-icpc北京現場賽D. Frog and Portal(構造)
阿新 • • 發佈:2018-11-17
題目連結:QAQ
題意:一直小青蛙位於標號為0的荷葉,他要跳到標號為200的荷葉,他每次只能想右跳一格或者兩格,現在有傳送門,如果x號的荷葉上建著傳送門,如果小青蛙到達x位置,就必須傳送到傳送門指定的位置,一個荷葉上只能建一個傳送門的起點。題目中指出如果沒有傳送門,小青蛙跳到每個荷葉的方案數符合斐波那契數列(其實並沒有什麼卵用)。現在給你一個到終點的方案數,讓你構造傳送門。
思路:唉,比賽的時候太緊張了,一直按著錯誤的方向想,這該死的斐波那契數列。
(用二進位制進行構造)其實構造的操作差不多都想到了,就是沒想到一起實現。
詳細構造方法見程式碼。
附上程式碼
#include<stdio.h> #include<vector> #include<algorithm> using namespace std; struct inst { int x; int y; }; inst ax[300]; int main(void) { long long n; while (scanf("%lld", &n) != EOF) { if (n == 0) { printf("2\n"); printf("1 1\n"); printf("2 1\n"); continue; } else if (n == 1) { printf("2\n"); printf("1 199\n"); printf("2 2\n"); continue; } int t = 0; int cnt = 0; while (n != 1) { int z = n % 2; if (z == 1) { n = n - 1; ax[cnt].x = t+1; ax[cnt].y = 199; cnt++; t = t + 2; } else { n = n / 2; ax[cnt].x = t + 1; ax[cnt].y = t + 3; cnt++; ax[cnt].x = t + 2; ax[cnt].y = t + 1; cnt++; t = t + 3; } } ax[cnt].x = t; ax[cnt].y = 199; cnt++; printf("%d\n", cnt); for (int i = 0; i < cnt; i++) { printf("%d %d\n", ax[i].x, ax[i].y); } } return 0; }