A Little Keng(快速求幂)
题目大意:计算1
n + 2
n + 3
n + ... + m
n 的值的末尾有多少个0,其中(1 <= m <= 100 , 1 <= n <= 1000000)
解题思路:看到m
n翻书找数论的知识,发现和快速求幂很像,想了一下那个mod应该是不能超过int,就用了平时最喜欢用的1000000000,具体原因不知道,直接套用了模板
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
using namespace std;
const int mod = 1000000000;
long long f(int m,int n)
{
if(n == 1) return m;
long long ret = f(m,n/2);
ret = (ret*ret) % mod;
if(n&1)
ret = (ret*m) % mod;
return ret;
}
int main()
{
int n, m;
while(scanf("%d%d",&m,&n) != EOF)
{
long long sum = 0;
for(int i = 1; i <= m; i++)
{
sum = sum + f(i,n);
}
int ans = 0;
while(sum%10 == 0 && sum != 0)
{
ans++;
sum /= 10;
}
printf("%d\n", ans);
}
return 0;
}<span style="font-family:SimSun;font-size:10px;"><strong>
</strong></span>
题目大意:给定一个立体的图形,上面是圆柱,下面是圆台,圆柱的底面半径和圆台的上半径相等,然后体积的V时,问这个图形的表面积最小可以是多少。(不算上表面)。据说是三分,没想出怎么做
C Bloodsucker(概率DP)
题目大意:第0天有n-1个人,1个吸血鬼,每天只能有2个生命相遇,若为同类则没反应,若为1人1鬼,则人变成鬼的概率为p,给出n和p,求所有人变成吸血鬼的期望
解题思路:逆推,dp[i]代表i个人到所有人变成吸血鬼的概率,往上逆推,则:dp[i]=(dp[i+1]+1)*p1+(dp[i]+1)*(1-p1)
移项后化简得: p1*dp[i]=dp[i+1]*p1+1
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
using namespace std;
double dp[100005];
int main()
{
int t,n;
double p;
scanf("%d",&t);
while(t--)
{
scanf("%d%lf",&n,&p);
dp[n] = 0;
for(int i = n-1; i >= 1; i--)
{
double s1,s2,p1;
s1 = (double)n*(n-1)/2;//从n个人中选两个人出来的选法
s2 = (double)i*(n-i);//为从i个人中选出一人的方法,n-i为从吸血鬼中选出一个吸血鬼的总数,共有这么多种
p1 = s2/s1*p;//人与吸血鬼相遇的概率
dp[i] = (dp[i+1]*p1+1)/p1;
}
printf("%.3f\n",dp[1]);
}
return 0;
}
H How Many Sets I(容斥原理+快速求幂)
题目大意:一个有n个元素的集合S,其子集为Si, 求有多少个有序的(S 1, S 2, ..., S k) 组合满足S 1 ∩ S 2 ∩ ... ∩ S k = ∅,题目给出n和k
解题思路:总情况数减去有交集的子集数。
2^n^k - C(n, 1)*2^(n-1)^k + C(n, 2)*2^(n-2)^k ... C(n, n)*2^(n-n)^k 化简为(2^k - 1)^n,再 两次快速幂
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
using namespace std;
const int mod = 1000000007;
int powMod(long long a,int b)
{
long long res=1;
while(b)
{
if(b&1)
res = (res*a)%mod;
b >>= 1;
a = (a*a)%mod;
}
return res;
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k) != EOF)
printf("%d\n", powMod((powMod(2, k)-1+mod)%mod, n));
return 0;
}
本文精选了算法竞赛中的三道典型题目,包括快速幂运算、概率动态规划及组合数学问题,详细介绍了每道题目的背景、解题思路及代码实现。
1015

被折叠的 条评论
为什么被折叠?



