网易编程题:小易喜欢的数列

题目:https://www.nowcoder.com/practice/49375dd6a42d4230b0dc4ea5a2597a9b?tpId=90&tqId=30787&tPage=1&rp=1&ru=/ta/2018test&qru=/ta/2018test/question-ranking

#include

using namespace std;

/*从后往前递归,超时*/

int calc(int pre_num,int curlen,int k){

int sum = 0;

if(curlen == 1)

return 1;

for(auto i = 1;i < pre_num;++i){

if(pre_num % i != 0)

sum += calc(i,curlen - 1,k);

}

for(auto i = pre_num;i < k+1;++i)

sum += calc(i,curlen - 1,k);

return sum;

}

int main(){

int n,k;

while(cin >> n >> k){

cout<

}

    return 0;

}


解法二:DP

#include

using namespace std;

const int mod = 1000000007;

int func(int n,int k){

/*dp[i][j]表示长度为i,以j结尾的序列数*/

vector< vector > dp(n+1,vector(k+1,0));

for(auto i = 1;i < k+1;++i)

dp[1][i] = 1;

for(auto i = 2;i < n+1;++i){

int sum = 0;

for(auto j = 1;j < k+1;++j){

sum  = (sum + dp[i-1][j]) % mod;

}

for(auto j = 1;j < k+1;++j){

int p = 2;

int invalid = 0;

while(p * j < k+1){

invalid = (invalid + dp[i-1][p*j] % mod) % mod;

++p;

}

dp[i][j] = (sum - invalid + mod) % mod;

}

}

int ans = 0;

for(auto i = 1;i < k+1;++i)

ans = (ans + dp[n][i]) % mod;

return ans;

}

int main(){

int n,k;

while(cin >> n >> k){

cout<

}

return 0;

}

你可能感兴趣的:(网易编程题:小易喜欢的数列)