给定表达式[x/2] + y + x * y, 其中x,y都是正整数。其中的中括号表示下取整,例如[3/2] = 1 , [5/2] = 2。 有些正整数可以用上述表达式表达出来,例如正整数2,当取x = y = 1时,可以把2表达出来 ( 解释下:当x=y=1时, [x / 2] + y + x * y = [1 / 2] + 1 + 1 * 1 = 0+1+1 = 2 ); 有些数可以有多种方式表达,例如13可以由 x = 2 y = 4 以及x = 3 y = 3来表示; 有些数无法用这个表达式表达出来,比如3。 从1开始第n个不能用这个表达式表示出来的数,我们叫做an,例如a1=1 a2=3,给定n,求an。 输入:n值 1<=n<=40 输出:an % 1000000007的结果(因为结果较大,输出an %1000000007的结果)。 函数头部 C/C++: int givean(int n); java: public class Main { public static int givean(int n) { } }
#include <iostream>
#include <vector>
using namespace std;
class Node
{
public:
Node(int a, int b)
{
y = b;
value = (a>>1) + b + a*b;
}
void increase(int x)
{
y++;
value = (x>>1) + y + x*y;
}
int y;
long long int value;
};
int givean(int n)
{
vector<Node> buf;
buf.push_back(Node(0,0));
buf.push_back(Node(1,2));
buf.push_back(Node(2,1));
long long int preNum = 2;
long long int curNum = 2;
int preCount = 1;
int curCount = 1;
int result = 1;
while (curCount < n)
{
int x = 1;
int y = buf[1].y;
int curNum = buf[1].value;
int size = buf.size();
int i = 2;
for (; i < size; i++)
{
if ((buf[i].value) == preNum)
{
buf[i].increase(i);
}
if ((buf[i].value) < curNum)
{
x = i;
y = buf[i].y;
curNum = buf[i].value;
}
}
if (i == (size-1))
{
buf.push_back(Node(size,1));
}
buf[x].increase(x);
if (curNum != preNum+1)
{
curCount = preCount + (curNum-preNum-1);
if (curCount >= n)
{
result = preNum + (n-preCount);
break;
}
else
{
preCount = curCount;
}
}
preNum = curNum;
}
return result;
}
int main()
{
for (int i = 1; i <= 40; i++)
{
cout << givean(i) << endl;
}
return 0;
}