Raising Modulo Numbers
2015-08-24 09:15
302 查看
Description
Peoplearedifferent.Somesecretlyreadmagazinesfullofinterestinggirls'pictures,otherscreateanA-bombintheircellar,otherslikeusingWindows,andsomelikedifficultmathematicalgames.
Latestmarketingresearchshows,thatthismarketsegmentwassofarunderestimatedandthatthereislackofsuchgames.ThiskindofgamewasthusincludedintotheKOKODáKH.Therulesfollow:
EachplayerchoosestwonumbersAiandBiandwritesthemonaslipofpaper.Otherscannotseethenumbers.Inagivenmomentallplayersshowtheirnumberstotheothers.ThegoalistodeterminethesumofallexpressionsAiBifromallplayers
includingoneselfanddeterminetheremainderafterdivisionbyagivennumberM.Thewinneristheonewhofirstdeterminesthecorrectresult.Accordingtotheplayers'experienceitispossibletoincreasethedifficultybychoosinghighernumbers.
Youshouldwriteaprogramthatcalculatestheresultandisabletofindoutwhowonthegame.
Input
TheinputconsistsofZassignments.ThenumberofthemisgivenbythesinglepositiveintegerZappearingonthefirstlineofinput.Thentheassignementsfollow.Eachassignementbeginswithline
containinganintegerM(1<=M<=45000).Thesumwillbedividedbythisnumber.NextlinecontainsnumberofplayersH(1<=H<=45000).NextexactlyHlinesfollow.Oneachline,thereareexactlytwonumbersAiandBiseparatedbyspace.Bothnumbers
cannotbeequalzeroatthesametime.
Output
Foreachassingnementthereistheonlyonelineofoutput.Onthisline,thereisanumber,theresultofexpression
(A1B1+A2B2+...+AHBH)modM.
SampleInput
SampleOutput
Peoplearedifferent.Somesecretlyreadmagazinesfullofinterestinggirls'pictures,otherscreateanA-bombintheircellar,otherslikeusingWindows,andsomelikedifficultmathematicalgames.
Latestmarketingresearchshows,thatthismarketsegmentwassofarunderestimatedandthatthereislackofsuchgames.ThiskindofgamewasthusincludedintotheKOKODáKH.Therulesfollow:
EachplayerchoosestwonumbersAiandBiandwritesthemonaslipofpaper.Otherscannotseethenumbers.Inagivenmomentallplayersshowtheirnumberstotheothers.ThegoalistodeterminethesumofallexpressionsAiBifromallplayers
includingoneselfanddeterminetheremainderafterdivisionbyagivennumberM.Thewinneristheonewhofirstdeterminesthecorrectresult.Accordingtotheplayers'experienceitispossibletoincreasethedifficultybychoosinghighernumbers.
Youshouldwriteaprogramthatcalculatestheresultandisabletofindoutwhowonthegame.
Input
TheinputconsistsofZassignments.ThenumberofthemisgivenbythesinglepositiveintegerZappearingonthefirstlineofinput.Thentheassignementsfollow.Eachassignementbeginswithline
containinganintegerM(1<=M<=45000).Thesumwillbedividedbythisnumber.NextlinecontainsnumberofplayersH(1<=H<=45000).NextexactlyHlinesfollow.Oneachline,thereareexactlytwonumbersAiandBiseparatedbyspace.Bothnumbers
cannotbeequalzeroatthesametime.
Output
Foreachassingnementthereistheonlyonelineofoutput.Onthisline,thereisanumber,theresultofexpression
(A1B1+A2B2+...+AHBH)modM.
SampleInput
3 16 4 23 34 45 56 36123 1 23748593029382 17 1 318132
SampleOutput
2 13195 13
题解:快速幂+同余定理。
#include<stdio.h>
#include<string.h>
intc,d,n,m;
intpow(inta,intb)
{
intans=1;
<spanstyle="color:#ff0000;">a=a%m;//因</span><spanstyle="color:#cc0000;">为少写了这一句,WA了好多次!!
</span> while(b)
{
if(b&1)
ans=(ans%m*a%m)%m;
a=(a%m*a%m)%m;
b>>=1;
}
returnans;
}
intmain()
{
intT;
scanf("%d",&T);
while(T--)
{
scanf("%d",&m);
scanf("%d",&n);
intsum=0;
while(n--)
{
scanf("%d%d",&c,&d);
intt;
t=pow(c,d);
sum=(sum%m+t%m)%m;
}
printf("%d\n",sum);
}
return0;
}
相关文章推荐
- Open-Drain与Push-Pull
- chfn,chsh,last,login,mail ,mesg ,talk,wall,write,nice ,pstree ,renice,skill ,expr ,reset,tset,compress ,lpd ,lpq ,lpr ,lprm,fdformat ,mformat ,mkdosf
- chfn,chsh,last,login,mail ,mesg ,talk,wall,write,nice ,pstree ,renice,skill ,expr ,reset,tset,compress ,lpd ,lpq ,lpr ,lprm,fdformat ,mformat ,mkdosf
- Frick'ing Terrain Mesh!
- Training JTAG Interface
- ZOJ 3780 Paint the Grid Again 模拟
- 2015 Multi-University Training Contest 10 CRB and String
- 2015 Multi-University Training Contest 7 Clock
- 2015 Multi-University Training Contest 7 Gray code
- 解题报告 之 HDU5319 Painter
- See LCS again 最长递增子序列到最长公共子序列的转化
- TurboMail邮件系统优质售后创造产品价值
- MyBatis批量操作报错:Parameter 'xxxList' not found. Avail
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Minimization dp
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Order Book 模拟
- POJ 1995 Raising Modulo Numbers(快速幂取余)
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2) Array 模拟
- NMAKE : fatal error U1045: spawn failed : Permission denied
- 1079. Total Sales of Supply Chain (25) -记录层的BFS改进
- 1079. Total Sales of Supply Chain (25) -记录层的BFS改进