A:  HDU5170

这题让比较a^b与c^d的大小。1<=a,b,c,d<=1000.

显然这题没法直接做,要利用对数来求,但是在math库中有关的对数函数返回的都是浮点数,所以这又要涉及到eps问题。

其它就没有什么需要注意的了,我用的是log()函数,当然还可以用log10().....,原理不变。

#include <iostream>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <stack>
#define inf 0x3f3f3f3f
#include <stdio.h>
#include <string.h>
typedef long long ll;
#define mod 10000007
#define eps 1e-9
using namespace std;
int a,b,c,d;
int main()
{
    while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF)
    {
        if(b*log(a)-d*log(c)>eps)
            printf(">\n");
        else if(fabs(b*log(a)-d*log(c))<=eps)
            printf("=\n");
        else printf("<\n");
    }
    return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

 B:  HDU5171

题目:按照规则扩展一个集合k次,然后求其总和。

【分析】

扩展规则很简单,就是一个斐波那契数列,但是如果按照模拟的方法手动推算,复杂度对于本题的数据范围来说是不太合适的。(1≤k≤1000000000)

可以利用矩阵快速幂来迅速完成。(矩阵快速幂可以完成任何递推公式)

                            [0, 1, 0] 

[f[n-1],f[n],s[n-1]]*[1, 1, 1] = [f[n],f[n+1],s[n]]

                            [0, 0, 1]

我第一次写完代码后验证结果是对的,但提交一直WA,之后发现在计算矩阵A的k+1次幂时,发现中间爆数据了,果断把int a[3][3]改成了 __int64 a[3][3],果断A了。

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
typedef __int64 ll;
#define mod 10000007
using namespace std;
struct ma
{
    ll a[3][3];
}res,init;
int n,se[100010];
ll sum,k;
ma mult(ma x,ma y)
{
    ma temp;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            temp.a[i][j]=0;
            for(int z=0;z<3;z++)
            temp.a[i][j]=(temp.a[i][j]+x.a[i][z]*y.a[z][j])%mod;
        }
    }
    return temp;
}
ma Pow(ma x,ll ke)
{
    ma temp;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            temp.a[i][j]=(i==j);
        }
    }
    while(ke)
    {
        if(ke&1) temp=mult(x,temp);
        ke>>=1;
        x=mult(x,x);
    }
    return temp;
}
int main()
{
    while(scanf("%d%I64d",&n,&k)!=EOF)
    {
        sum=0;
        init.a[0][0]=0,init.a[0][1]=1,init.a[0][2]=0;
        init.a[1][0]=1,init.a[1][1]=1,init.a[1][2]=1;
        init.a[2][0]=0,init.a[2][1]=0,init.a[2][2]=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&se[i]);
        }
        sort(se,se+n);
        for(int i=0;i<n-2;i++)
        {
            sum=(sum+se[i])%mod;
        }
        k+=1;
        res=Pow(init,k);
        sum=(sum+(se[n-2]*res.a[0][2])%mod+(se[n-1]*res.a[1][2])%mod+(se[n-2]*res.a[2][2])%mod)%mod;
        printf("%I64d\n",sum);
    }
    return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.