【菜鸟er】模板专题_幂运算快速取模

#include  <bits/stdc++.h>
using namespace std;

#define LL long long
LL power_mod(LL a,LL b, LL mod)
{
    LL res = 1;
    a %= mod;

    while(b){
        if(b & 1) res = (res * a)%mod;

        b<<=1;//等效于b/=2;
        a = (a*a) % mod;
    }
    return res;
}


int main()
{
    LL a,b,mod;
    cin>>a>>b>>mod;
    int res = power_mod(a,b,mod);
    cout<<res<<endl;
    return 0;
}

高精度数幂运算

10-23

ExponentiationrnTime Limit: 500MS Memory Limit: 10000K rnTotal Submissions: 93983 Accepted: 22472 rnrnDescriptionrnrnProblems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. rnrnThis problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25. rnInputrnrnThe input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.rnOutputrnrnThe output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.rnSample Inputrnrn95.123 12rn0.4321 20rn5.1234 15rn6.7592 9rn98.999 10rn1.0100 12rnrnSample Outputrnrn548815620517731830194541.899025343415715973535967221869852721rn.00000005148554641076956121994511276767154838481760200726351203835429763013462401rn43992025569.928573701266488041146654993318703707511666295476720493953024rn29448126.764121021618164430206909037173276672rn90429072743629540498.107596019456651774561044010001rn1.126825030131969720661201rnHintrnrnIf you don't know how to determine wheather encounted the end of input: rns is a string and n is an integer rnrnC++rnwhile(cin>>s>>n)rnrn...rnrncrnwhile(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you wantrn/*while(scanf(%s%d",s,&n)!=EOF) //this also work */rnrn...rnrnSourcernrnrnrnrnrnrn我的源码如下,测试数据没有错误,但是提交网站的时候总报错,不知道为什么,各位大侠救救我!rnrn#include rn#includern#include rn#define maxsize 200rnusing namespace std;rnint main()rnrn int c[maxsize]=0,a[maxsize]=0;rn int g1=0,g2=0,n,i,j,k2=0,k1,g=0,k3=0,r=0,g3=0;rn char R[maxsize];rnrn while(cin>>R>>n)rn rn k1=strlen(R);rn for(i=0;i=10||jk3) k3=k2-1;//考虑是纯小数的情形前要加零rn while(c[g3]==0) g3++;//考虑后面都是零的情形要去掉rnrn for(i=k3;i>=g3;i--)rn rn if(g&&(i+1==k2))//小数点位置的判断rn rn cout<<".";rn cout<=0;i--)c[i]=0;//初始化c数组和各种参数rn rn g1=0,g2=0,k2=0,g=0,k3=0,r=0,g3=0;rn cout<

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试