牛客練習賽43 Tachibana Kanade Loves Probability(快速冪)
阿新 • • 發佈:2019-04-06
blank frame clas ref des get cst -c 來源
“設箱子內有 n 個球,其中給 m 個球打上標記,設一次摸球摸到每一個球的概率均等,求一次摸球摸到打標記的球的概率”
“emmm...語言入門題”
但是她改了一下詢問方式:設最終的答案為 p ,請輸出 p 小數點後 K1K1 到 K2K2 位的所有數字(若不足則用 0 補齊)
鏈接:https://ac.nowcoder.com/acm/contest/548/B
來源:牛客網
題目描述
立華奏在學習初中數學的時候遇到了這樣一道大水題:“設箱子內有 n 個球,其中給 m 個球打上標記,設一次摸球摸到每一個球的概率均等,求一次摸球摸到打標記的球的概率”
“emmm...語言入門題”
但是她改了一下詢問方式:設最終的答案為 p ,請輸出 p 小數點後 K1K1 到 K2K2 位的所有數字(若不足則用 0 補齊)
輸入描述:
第一行一個整數 T,表示有 T 組數據。
接下來每行包含四個整數 m,n,K1,K2m,n,K1,K2,意義如「題目描述」所示。
輸出描述:
輸出 T 行,每行輸出 K2−K1+1K2−K1+1 個數,表示答案。
註意同行的數字中間不需要用空格隔開。
示例1
輸入
5
2 3 2 3
1 7 1 7
2 5 1 3
12345 54321 3 10
12345 54321 100000 100010
輸出
66
1428571
400
72601756
78428232175
思路:求第k1位的數字 其實就是 m*10^(k-1)對n求模 所以用快速冪處理一下 然後就模擬短除法就行了
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> #define ll long long int using namespace std; inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;} int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1}; int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1}; const int inf=0x3f3f3f3f; const ll mod=1e9+7; ll m,n,k1,k2; ll q_pow(ll a,ll n,ll mod){ ll ans=m; ll base=a; while(n){ if(n&1) ans=(ans*base)%mod; base=base*base%mod; n>>=1; } return ans; } int main(){ //ios::sync_with_stdio(false); int t; scanf("%d",&t); while(t--){ scanf("%lld%lld%lld%lld",&m,&n,&k1,&k2); ll f=q_pow(10,k1-1,n); ll i=k1; while(1){ f*=10; printf("%lld",f/n); f%=n; i++; if(i==k2+1) break; } printf("\n"); } }
牛客練習賽43 Tachibana Kanade Loves Probability(快速冪)