2047. Maths
Time limit: 1.0 second
Memory limit: 64 MB
Android Vasya attends Maths classes. His group started to study the number theory recently. The teacher gave them several tasks as a homework. One of them is as follows.
There is an integer n. The problem is to find a sequence of integers
a1,…,an
such that for any
k
from
Input
The only line contains an integer n(2≤n≤100000) .
Output
If there is no such sequence output “Impossible” . Otherwise output space-separated integers a1,…,an(1≤ai≤300) .
Sample
input
3
output
1 3 4
题意
输入一个
n
,构造一个长度为
题解
首先暴力打个表,看看按照要求能够造出的最长的序列是多少,发现可以构造出长度为
100000
的序列,记下满足条件的最小的前缀和,然后打表逆推就行了,对于任意一个
ak
,
ak−1=sumk−num[sumk]
,
num[sumk]
是
sumk
的因数个数。(被爆栈折磨了一晚上。。。)
打表代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
using namespace std;
int num[30000005];
int MAX = 100000000;
int dfs(int n, int sum)
{
if (n == 100000)
{
MAX = min(MAX, sum);
return 1;
}
for (int i = 1; i <= 300; i++)
{
if (sum + i>30000005)
return 0;
if (num[sum + i] == i&&dfs(n + 1, sum + i))
return 1;
}
return 0;
}
int main()
{
memset(num, 0, sizeof num);
for (int i = 1; i <= 30000005; i++)
{
for (int j = i; j <= 30000005; j += i)
num[j]++;
}
for (int i = 1; i <= 300; i++)
{
dfs(1, i);
}
printf("%d\n", MAX);
getchar();
return 0;
}
AC代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
using namespace std;
int num[1586336];
int ans[100005];
int n;
int main()
{
memset(num,0,sizeof num);
for(int i=1;i<=1586335;i++)
{
for(int j=i;j<=1586335;j+=i)
num[j]++;
}
int sum=1586335;
for(int i=100000;i>0;i--)
{
ans[i]=num[sum];
sum-=num[sum];
}
scanf("%d",&n);
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
return 0;
}