ZZNU 1988 (大數取餘)

2021-07-22 03:00:24 字數 790 閱讀 9065

時間限制: 1 sec  記憶體限制: 128 mb

提交: 19  解決: 8

[提交][狀態]

給你兩個數 n, p(0 < n,p <= 10^15);

a1 = 1; 

a2 = 1+2; 

a3 = 1+2+3; 

...an = 1+2+3+...+n 

sn = a1+a2+a3+...+an;

求(6*sn) % p;

輸入乙個數 t表示有t組例項;

每組樣例輸入兩個整數 n , p

輸出結果;

2

1 1234567

2 1234567

6

24

題意: 當然我們先得知道 1+3+6+10+15+.......的求和公式sn=n*(n+1)*(n+2)/6;

所以這道題就是求:n*(n+1)(n+2)%p

當然沒有那麼簡單 直接求會爆long long ;

這裡運用了遞迴和同餘定理的一些技巧:

#include#include#include#include#include#include#include#includetypedef long long ll;

using namespace std;

#define inf 0x3f3f3f3f

ll mul(ll a,ll b,ll p)

int main()

return 0;

}

lightoj1214 大數取餘

given two integers,aandb,you should check whetherais divisible bybor not.we know that an integerais divisible by an integerbif and only if there exist...

九的餘數 105 (大數取餘)

時間限制 3000 ms 記憶體限制 65535 kb 難度 3 描述 現在給你乙個自然數n,它的位數小於等於一百萬,現在你要做的就是求出這個數整除九之後的餘數。輸入 第一行有乙個整數m 1 m 8 表示有m組測試資料 隨後m行每行有乙個自然數n。輸出輸出n整除九之後的餘數,每次輸出佔一行。樣例輸入...

C語言程式設計練習28 大數取模

題目描述 現給你兩個正整數a和b,請你計算a mod b。為了使問題簡單,保證b小於100000。輸入 輸入包含多組測試資料。每行輸入包含兩個正整數a和b。a的長度不超過1000,並且0輸出 對於每乙個測試樣例,輸出a mod b。樣例輸入 copy 2 3 12 7 152455856554521...