题目
代码
D 1 D_1 D1 n s q r t n nsqrtn nsqrtn做法:整除分块:
using namespace std;
const int INF = 0x3f3f3f3f;
int dp[200010];
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
int pre=0;
dp[n]=1;
for(int i=n;i>=1;i--){
dp[i]=(dp[i]+pre)%m;
int l=1,r;
while(l <= i){
r = i/(i/l);
// cout<<r<<" "<<l<<" "<<i/l<<" "<<dp[i]<<endl;
if(i/l!=i) dp[i/l] =(dp[i/l] + (r-l+1)*dp[i]%m)%m;
// cout<<"! "<<i/l<<" "<<dp[i/l]<<endl;
l = r+1;
}
// for(int j=1;j<=sqrt(n)&&j!=i;j++){
// dp[j]=(dp[j]+dp[i])%m;
// cout<<"!"<<j<<" "<<dp[j]<<endl;
// }
//cout<<dp[i]<<" "<<pre<<endl;
pre=(pre+dp[i])%m;
}
cout<<dp[1]<<endl;
return 0;
}
D 2 D_2 D2 n l o g n nlogn nlogn做法:
using namespace std;
const int INF = 0x3f3f3f3f;
int dp[4000010],pre[4000010];
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
dp[n]=1;
for(int i=n;i>=1;i--){
dp[i]=(dp[i]+pre[i+1])%m;
int l=1,r;
for(int j=2;i*j<=n;j++){
dp[i]=(dp[i]+pre[i*j]-pre[min(n+1,i*j+j)]+m)%m;
}
pre[i]=(pre[i+1]+dp[i])%m;
}
cout<<dp[1]<<endl;
return 0;
}