zoukankan      html  css  js  c++  java
  • HDOJ 1097(幂取模)

    //超时代码,需要o(logn)
    #include <stdio.h>
    int main()
    {
    int m,n,i,ans=1;
    while (scanf("%d%d",&m,&n)!=EOF)
    {
    for (i=1;i<=n;i++)
    {
    ans*=m;
    ans%=10;
    }
    printf("%d\n",ans);
    }
    }

    #include<stdio.h>
    long pow_mod(int m, int n, int p)
    {
    //if(!n) return 1;
    long temp;
    long ans;
    if(0==n)
    return 1;
    temp = pow_mod(m, n/2, p);
    ans = temp * temp % p;
    if(n&1) ans = ans * m % p;
    return ans;
    }
    int main()
    {
    long m, n;
    while(~scanf("%ld%ld", &m, &n))
    printf("%ld\n", pow_mod(m, n, 10));
    return 0;
    }

     我发现全按long可以
    m n不安long会停止


    //也对

    int pow_mod(int x,int y,int p)
    {
    long long t=x;
    long long ans=1;
    while(y)
    {
    if(y&1)
    ans=t*ans%p;
    t=t*t%p;
    y=y>>1;
    }
    return (int)ans;
    }*/
    int main()
    {
    int m,n;
    while(~scanf("%d%d",&m,&n))
    printf("%d\n",pow_mod(m,n,10));
    return 0;
    }


    //运用abmodp=(amodp)*(bmodp)modp

    自己用个变量打印一下递归调用次数你就知道了

  • 相关阅读:
    json数据读取后自动进行模板生成
    json数据排序
    fastjson json数据处理
    xml数据转Json
    jackson-dataformat-xml xml转json
    jar工具打包
    工具操作
    IBM MQ
    RabbitMq
    Excel4J
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2456097.html
Copyright © 2011-2022 走看看