1、快速幂:
问题 A: Digits
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]题目描述
给一个关于x的多项式,并给定一个x,求该多项式在带入该x时的值最后k位数字。
输入
第一行两个整数 n、k;
之后的 n 行,每行两个数 ai和bi,表示多项式的一项 aixbi;
最后一行一个整数x。输出
输出 k 行,按顺序输出该多项式带入x后值的最后 k 位数字,若不足 k 位,则高位补零。
样例输入 Copy
2 1 3 2 1 5 3样例输出 Copy
0提示
对于 100%的数据,1≤n≤100000,1≤ai,bi,x≤109,1≤k≤8。
解题思路 : 这道题思路明显
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e5+5;
int A[mod],B[mod];
int main()
{
int n,k,x;
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d %d",&A[i],&B[i]);
}
scanf("%d",&x);
int p=1;
for(int i=1;i<=k;i++)
p*=10;
ll sum=0;
for(int i=1;i<=n;i++)
{
ll ans=1;
ll a=x,b=B[i];//A*x^b
while(b)
{
if(b%2==1) ans=ans*a%p;
a=a*a%p;
b/=2;
}
ans*=A[i]%p;
sum=(sum+(ans%p))%p; //注意这里使用的快速幂
}
ll num[20]={0},cnt=0; //注意输出
while(sum)
{
num[++cnt]=sum%10;
sum/=10;
}
for(int i=k;i>=1;i--)
{
if(i!=k) printf("\n");
printf("%lld",num[i]);
}
return 0;
}
/**************************************************************
Problem: 4017
User: 2019UPC110
Language: C++
Result: 正确
Time:116 ms
Memory:2804 kb
****************************************************************/
2、期望
问题 G: Dice in Line
时间限制: 1 Sec 内存限制: 128 MB Special Judge
[提交] [状态]题目描述
We have N dice arranged in a line from left to right. The i-th die from the left shows pi numbers from 1 to pi with equal probability when thrown.
We will choose K adjacent dice, throw each of them independently, and compute the sum of the numbers shown. Find the maximum possible value of the expected value of this sum.
Constraints
·1≤K≤N≤200000
·1≤pi≤1000
·All values in input are integers.输入
Input is given from Standard Input in the following format:
N K
p1 ... pN输出
Print the maximum possible value of the expected value of the sum of the numbers shown.
Your output will be considered correct when its absolute or relative error from our answer is at most 10−6.样例输入 Copy
【样例1】 5 3 1 2 2 4 5 【样例2】 4 1 6 6 6 6 【样例3】 10 4 17 13 13 12 15 20 10 13 17 11样例输出 Copy
【样例1】 7.000000000000 【样例2】 3.500000000000 【样例3】 32.000000000000提示
样例1解释
When we throw the third, fourth, and fifth dice from the left, the expected value of the sum of the numbers shown is 7. This is the maximum value we can achieve.
样例2解释
Regardless of which die we choose, the expected value of the number shown is 3.5.
题目大意:求连续一段的值,emmm,算期望
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 2e5+5;
int A[mod];
int main()
{
int n,k;
A[0]=0;
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&A[i]);
A[i]+=A[i-1];
}
double ans=0;
int sum=0;
for(int i=k;i<=n;i++)
{
sum=max(sum,A[i]-A[i-k]);
}
ans=0.5*(sum+k);
printf("%0.8lf",ans);//////////很重要
return 0;
}
/**************************************************************
Problem: 14642
User: 2019UPC110
Language: C++
Result: 正确
Time:26 ms
Memory:2804 kb
****************************************************************/