EOJ Monthly 2019.9 (based on September Selection) - C. 划水

- ECNU 華東師大月賽C題 -

C. 划水

單點時限: 2.0 sec | 內存限制: 256 MB

題目描述

“向右看齊”
“向前看”
“ 20 分鐘軍姿”
每天的軍訓, Cuber QQ 最喜歡的就是站軍姿的環節。因爲在站軍姿的時候, Cuber QQ 可以看着美麗的麗娃河思考人生。
今天, Cuber QQ 開始觀察麗娃河上的鴨子了。 Cuber QQ 近似地把麗娃河看成一個圓形的池塘,他數了數,一共有 n 只鴨子在麗娃河上,鴨子在麗娃河上任意的划水。
Cuber QQ 突發奇想,如果它們的位置是等概率隨機的,它們有多大的概率會分佈在圓形池塘的同一個半圓內呢?
“眼睛都別閉上了,我看看誰的眼睛閉着讓他站到前面來”
教官突然的一句話,打斷了 Cuber QQ 的思考,他忘記怎麼做了。

輸入格式

第一行一個整數 T ( 1≤T≤105 ),代表數據組數。
接下來 T 行,每行一個整數 n ( 1≤n≤1018 ),代表鴨子的數量。

輸出格式

共 T 行,每行一個整數,表示答案。
可以證明答案是一個分數。我們令答案的最簡分數是 P/Q,對每一個詢問輸出 P⋅Q−1mod1 000 000 007,其中 Q−1 是 Q 在模 1 000 000 007 意義下的逆元。

樣例

Input
2
1
2

Output
1
1

提示

鴨子數量爲 1 或 2 時,屬於同一個半圓的概率是 1。
由於鴨子相比池塘太小了,我們可以把鴨子當作點。

題解

題解自己想的時候以爲概率是(1/21-n)  ̄□ ̄||

代碼

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
#define inf 1ll<<60
#define zero 1e-7
//n 只鴨子分佈在同一個半圓中的概率就是 n*2^(1−n)。
//求逆元 inva≡a^(p−2)(mod p)→ 費馬小定理
typedef long long ll;
const int N=1e5+5;
const int maxn=5e3+5;
const ll mod=1e9+7;

ll power(ll a, ll k) {
    ll b=1;
    while(k) {
        if(k&1) b=b*a%mod;
        a=a*a%mod;
        k>>=1;
    }
    return b%mod;
}

int main() {
    int t;
    ll n;
    scanf("%d", &t);
    while(t--) {
        scanf("%lld", &n);
        ll res;
        if(n<=2) res=1;
        else {//注意看題:最簡分數,所以P和Q(即n和temp)要約分
            ll temp=power(2, n-1);
            ll g=__gcd(n%mod, temp);
            n=n%mod/g;
            temp/=g;
            res=n*power(temp, mod-2)%mod;
        }
        printf("%lld\n", res);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章