URAL2047 Maths (暴力打表 递推)

来源:互联网 发布:淘宝最大的浏览单平台 编辑:程序博客网 时间:2024/05/18 02:18

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 2 to n the sum a1++ak has exactly ak different positive divisors. Help Vasya to cope with this task.

Input

The only line contains an integer n(2n100000).

Output

If there is no such sequence output Impossible. Otherwise output space-separated integers a1,,an(1ai300).

Sample

input

3

output

1 3 4

题意

输入一个n,构造一个长度为n的序列,要求是对于任意一个ak(2kn),a1+a2+......+akak个因数(a300)

题解

首先暴力打个表,看看按照要求能够造出的最长的序列是多少,发现可以构造出长度为100000的序列,记下满足条件的最小的前缀和,然后打表逆推就行了,对于任意一个ak,ak1=sumknum[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;}
0 0
原创粉丝点击