Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he asks rikka to have some practice on codeforces. Then she opens the problem B:
Given an integer K , she needs to come up with an sequence of integers A satisfying that the number of different continuous subsequence of A is equal to k .
Two continuous subsequences a, b are different if and only if one of the following conditions is satisfied:
1. The length of a is not equal to the length of b .
2. There is at least one t that at≠bt , where at means the t -th element of a and bt means the t -th element of b .
Unfortunately, it is too difficult for Rikka. Can you help her?
Given an integer K , she needs to come up with an sequence of integers A satisfying that the number of different continuous subsequence of A is equal to k .
Two continuous subsequences a, b are different if and only if one of the following conditions is satisfied:
1. The length of a is not equal to the length of b .
2. There is at least one t that at≠bt , where at means the t -th element of a and bt means the t -th element of b .
Unfortunately, it is too difficult for Rikka. Can you help her?
Input
There are at most 20 testcases,each testcase only contains a single integer
K (1≤K≤109)
Output
For each testcase print two lines.
The first line contains one integers n (n≤min(K,105)) .
The second line contains n space-separated integer Ai (1≤Ai≤n) - the sequence you find.
The first line contains one integers n (n≤min(K,105)) .
The second line contains n space-separated integer Ai (1≤Ai≤n) - the sequence you find.
Sample Input
10
Sample Output
4 1 2 3 4
脑洞题,直接假设数列只有2个数,并且是连续的一段1,然后是连续的一段2,最后是连续的一段1
于是假设三段的长度分别为a,b,c且(a>=c)于是不同的序列总数为ab+bc+ac+a+b
然后只要让ab+bc+ac+a+b=k就好了
化简一下(a+c+1)*(b+c+1)=k+(c+1)*(c+1)
然后枚举(c+1)同时保证a>=c且a+b+c<=100000即可,貌似没有可以卡的数据
#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map> #include<algorithm> #include<string> #pragma comment(linker, "/STACK:102400000,102400000") typedef long long ll; using namespace std; const ll maxn=100005; int T,n,m,f[maxn]; int main() { //scanf("%d",&T); for (int i=1;i<=33000;i++) f[i]=i*i; while (~scanf("%d",&n)) { /*if (n<=100000) { for (int i=1;i<=n;i++) { putchar('1'); if (i<n) putchar(' '); else putchar(10); } } else */ { int k,u,a,b,c,flag=0; for (int i=1;i<=33000;i++) { k=n+f[i]; u=sqrt(1.0*k); u=min(u,k/(i+i-1)); for (int j=u;j>=i;j--) if (k%j==0) { c=i-1; b=j-i; a=k/j-i; if (a+b+c<=100000) flag=true; if (flag) break; } if (flag) break; } printf("%d\n",a+b+c); flag=0; for (int i=1;i<=a;i++) { if (flag) printf(" "); else flag=1; printf("1"); } for (int i=1;i<=b;i++) { if (flag) printf(" "); else flag=1; printf("2"); } for (int i=1;i<=c;i++) { if (flag) printf(" "); else flag=1; printf("1"); } putchar(10); } } return 0; }