• Openjudge/Poj 1183 反正切函数的应用


    1.链接地址:

    http://bailian.openjudge.cn/practice/1183

    http://poj.org/problem?id=1183

    2.题目:

    总时间限制:
    1000ms
    内存限制:
    65536kB
    描述
    反正切函数可展开成无穷级数,有如下公式

    (其中0 <= x <= 1) 公式(1)

    使用反正切函数计算PI是一种常用的方法。例如,最简单的计算PI的方法:

    PI=4arctan(1)=4(1-1/3+1/5-1/7+1/9-1/11+...) 公式(2)

    然而,这种方法的效率很低,但我们可以根据角度和的正切函数公式:

    tan(a+b)=[tan(a)+tan(b)]/[1-tan(a)*tan(b)] 公式(3)

    通过简单的变换得到:

    arctan(p)+arctan(q)=arctan[(p+q)/(1-pq)] 公式(4)

    利用这个公式,令p=1/2,q=1/3,则(p+q)/(1-pq)=1,有

    arctan(1/2)+arctan(1/3)=arctan[(1/2+1/3)/(1-1/2*1/3)]=arctan(1)

    使用1/2和1/3的反正切来计算arctan(1),速度就快多了。
    我们将公式(4)写成如下形式

    arctan(1/a)=arctan(1/b)+arctan(1/c)

    其中a,b和c均为正整数。

    我们的问题是:对于每一个给定的a(1 <= a <= 60000),求b+c的值。我们保证对于任意的a都存在整数解。如果有多个解,要求你给出b+c最小的解。
    输入
    输入文件中只有一个正整数a,其中 1 <= a <= 60000。
    输出
    输出文件中只有一个整数,为 b+c 的值。
    样例输入
    1
    样例输出
    5
    来源
    Noi 01

    3.思路:

    数学题,暴力枚举会超时

    思路参考http://hi.baidu.com/sjzezoi/item/f563a11c6accf0dd65eabff8

    题目要求求出

    1 / a = (1 / b+1 / c) / (1 - 1 / (b * c) )

    ==> ab + ac = bc - 1

    令b=a+m, c=a+n

    ==> mn=a^2+1

    所以m或n必然小于a,且为正整数

    所以可以直接枚举m的值了,注意计算a*a可能爆longint

    4.代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     long long a;
     9     cin>>a;
    10 
    11     long long m;
    12     for(m = a; m > 0; --m)
    13     {
    14         if((a * a + 1) % m == 0) break;
    15     }
    16     cout<<(a * 2 + m + (a * a + 1) / m)<<endl;
    17 
    18     return 0;
    19 }
  • 相关阅读:
    bzoj2957 -- 线段树
    bzoj2209 [ JSOI2011 ] -- splay
    bzoj3874 [ AHOI2014 ] -- 爬山算法
    bzoj1038 [ ZJOI2008 ] -- 模拟退火+二分
    bzoj2428 [ HAOI2006 ] -- 模拟退火
    bzoj3680 -- 模拟退火
    bzoj4500 -- 差分约束
    bzoj3527 -- FFT
    bzoj1013 [ JSOI2008 ] -- 高斯消元
    使用nginx try_files 指令 管理静态资源
  • 原文地址:https://www.cnblogs.com/mobileliker/p/3549831.html
Copyright © 2020-2023  润新知