首页 技术 正文
技术 2022年11月19日
0 收藏 919 点赞 3,603 浏览 1412 个字

题目链接

这种题一看就是dp啊,dp[i][j]表示第i位放j的方案数,转移方程为dp[i][j]=dp[i-1][k]{k<=i||k%i!=0},当然我们可以三层循环来找,但数据显然会超时,那么我们只能在第二层循环中用中间变量记录一下可以省去一层循环,但是为倍数的情况必须要考虑,所以先预处理出所有的倍数,然后dp的过程中减去倍数的情况即可,总体复杂度O(n* k * sqrt(k)。

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <bitset>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAXN 1010100
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll __int64
#define INF 0x7fffffff
#define cs(s) freopen(s,"r",stdin)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
const LL mod=1e9+7;
LL dp[11][100003];
vector<LL>p[100001];
LL n,k;
void P(){//预处理倍数
for(int i=1;i<=k;i++){
for(int j=2;1ll*j*i<=k;j++){
p[i].pb(j*i);
}
}
}
LL a[111];
int main(){
ios::sync_with_stdio(false);
cin>>n>>k;
P();
for(int i=1;i<=k;i++)dp[1][i]=1;
for(int i=1;i<=n;i++)dp[i][1]=1;
for(int i=2;i<=n;i++){
LL pr=0;
for(int j=1;j<=k;j++)pr=(0ll+pr+dp[i-1][j])%mod;
LL sum=dp[i-1][1];
for(int j=2;j<=k;j++){
LL mi=pr;
dp[i][j]=(0ll+sum+dp[i-1][j])%mod;
sum=(0ll+sum+dp[i-1][j])%mod;//为dp[i-1][1]到dp[i-1][j]的和
mi-=sum;
for(int x:p[j]){
mi-=dp[i-1][x];//减去倍数
}
dp[i][j]+=mi;
}
}
LL ans=0;
for(int i=1;i<=k;i++)ans=(ans+dp[n][i])%mod;
cout<<ans;
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902