Getting in Shape

A question recommended by a classmate of the big brother has indeed been tossing for a long time, and then I got stuck on a question for a year..

Juan decided to start working out and is willing to prepare a workout session.

He knows that some days he might not want to do all exercises from his workout session. He decided on some rules to avoid skipping the whole session and not exercising at all, while still allowing him to optionally skip some exercises.

The rules are:

  • There will be only two types of exercises: AA and BB.
  • After finishing an exercise of type BB he moves to the next exercise, if there is one. Otherwise, the workout session ends.
  • After finishing an exercise of type AA there are two possibilities: he can move to the next exercise, or he can skip the next exercise and move to the one after that.
  • The last exercise in a workout session must always be of type BB.

Therefore, there might be different ways in which the workout session can be completed. For example, if the types of exercises in a workout session are BAABBAAB, there are 3 ways in which the session can be completed: by doing all exercises, by skipping the 3rd one or by skipping the last one.

Juan wants to prepare his workout session in such a way that there are exactly NN different ways in which the workout session can be completed. Can you help him?

Input

One positive integer NN (2≤N≤10152≤N≤1015), representing the number of ways in which the workout session can be completed.

Output

Output a line containing a string, formed only with characters 'A' and 'B', representing the types of the exercises in the workout session. If there are multiple valid answers, output the lexicographically smallest answer. If there is no valid workout sessions, output a line containing the string "IMPOSSIBLE" (without quotes).

        To sum up, the title needs to find a string that ends with B and contains only two characters, AB, and is n in total according to the rules, and needs to have the smallest lexicographical order among all legal strings.

        Assuming that the final string length is length, this question can be abstracted as the way to go to the length+1 position is n. Considering the total number of moves to reach any position i (i>2), it is actually only related to the first two digits, that is, num[i]=num[i-1]+num[i-2], which is very similar to Fibonacci Deed sequence, but the difference is that if the i-2 position is B, then in fact, the method number of num[i-2] cannot be superimposed to num[i] at this time, that is, num[i]=num[i-1 ] (if i-2 position is B).

         At this point, we should be able to see that B is actually quite special. Multiple consecutive Bs will not actually increase the number of methods, but simply pass down the number of methods. Therefore, no matter how many B are equal to one B, for the dictionary The order is the smallest, only need to consider what will happen if one joins one B.

        Observe A...AB(1)A...AB(2)A..., the continuous A is the number of routes in the first two positions, and the number of routes in A after B(1) In fact, it is the total number of routes in the previous A...AB(1) string, and then all these routes reach A behind B(1), and then start from these routes and pass down to B(2 ) is actually the product of the number of routes in the previous two A..AB strings, because I don’t care how you get to A after B(1), but each of these routes will correspond to A...AB(2 ) All routes conform to the principle of multiplication. More groups are actually the same reason, which is to multiply the number of individual routes in each string of A...AB, and each string of A...AB represents the Fibonacci sequence a number of .

        At this time, the idea of ​​​​solving the problem almost came out, as long as it is judged whether a number can be expressed as the product of the numbers in the Fibonacci sequence. For the largest lexicographical order, it should also start searching from the largest. At the beginning, I used a greedy algorithm to search from the largest, and use a loop to find the solution from the largest to the smallest, but later I found that it was not working, so I considered using dfs to search, that is, I found the factor, and then continued to search in this situation until I found it. Finally you can exit.

        Record a small problem (that is, the point of wa all the time), that is, the length of the dfs answer sequence length cnt+1 must be placed in the dfs, because it may find a large factor when it is placed outside and looping, but it turns out that it is not possible. That is, after adding a large factor, there is no answer at the end, but a small factor can be used (this is actually the reason why greed is not allowed), and it is necessary to find a combination of small factors, because the priority is to have an answer, but you add cnt in the search cycle. Yes, when the time comes to find the answer, it will continue to be superimposed later. The summary is: what needs to be backtracked must be placed in the parameters of dfs, so that it is not possible to find it later, so that it is convenient to return to the previous state.

Here is the code:

#include<stdio.h>
#define ll long long
ll a[1000];
int ans[1000];//答案
int stats = 0;//状态

void dfs(ll sr, int cnt, int k) {
	if (sr == 1) {
		for (int i = 1; i <= cnt-1; i++) {
			for (int j = 1; j <= ans[i] - 1; j++) {
				printf("A");
			}
			printf("B");
		}
		printf("\n");
		stats = 1;//找到答案了
		return;
	}
	for (int i = k; i >= 3; i--) {
		if (sr%a[i] == 0) {
			k = i;
			ans[cnt] = i - 1;//到时候A...B串的长度
            //cnt++; //必须要放到里面去,不然当前情况不行,后面可以了,但是cnt就是这个数字了
			dfs(sr/a[i], cnt+1, k);
			if (stats) {
				return;
			}
			//printf("n=%lld *a[i]=%d\n",n, a[i]);
		}
	}
}

int main() {
	ll n; scanf("%lld", &n);
	a[1] = 1;
	a[2] = 1;
	int k;
	for (int i = 3; i <= 1000; i++) {
		a[i] = a[i - 1] + a[i - 2];
		if (a[i] > n) {
			k = i - 1;
			break;
		}
	}
	//printf("%d\n", num);
	int cnt = 1;//答案长度
	dfs(n, cnt, k);
	if (!stats) {
		printf("IMPOSSIBLE\n");
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_60360239/article/details/127207114