看一下数学思路:
)
根据题目表述,必须分组使得每组宠物个数相同,即n%k==0
pow函数为内置函数,不了解的uu自己查一下即可
C++ 代码
#include <iostream>
#include <cmath>
using namespace std;
const double eps = 1e-6;
int n,K,ans;
double p;
double m = 1e9;
int main(){
cin>>n>>p;
for(int k=2;k<=n;k++){
if (n%k) continue;
double EX = (n/k) * (pow(1-p,k) + (1 - pow(1-p,k)) * (k+1));
if (m > EX - eps) ans = k, m = EX;
}
if (m > n-eps) cout<<1;
else cout<<ans;
return 0;
}