您的位置:首页 > 大数据 > 人工智能

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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: