2013 多校第十场 1009 sum 欧拉定理 快速幂 高精度

题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=4704

首先,这样的方法数就是c【N-1】【K-1】  就是可重复元素的组合数  ,然后求和后是2^N-1  计算mod 100000007  由于模是素数,由费马小定理,只需要计算N-1除以mod 的余数,然后计算余数的时候,据说BigInteger会超时,那就死算吧,把每一位是多少算出来。


注意要点: 1  虽然p【i】都是int 范围内的,但是在计算过程中可能溢出。  所以开long long 

                     2  算快速幂时也可能存在类似问题,所以参数都设置成long long 

#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
typedef long long inta;       
using  namespace std;

inta quick_mod(inta a,inta b,inta m)
{
   inta ans=1;
   a=a%m;
   while(b)
   {
      if(b&1)ans=(ans*a)%m;
      a=(a*a)%m;
      b>>=1;
   }

   return ans;
}

long long  p[100001];

int mod =1000000006;
int modp=1000000007;

void pre()
{
   p[0]=1;
   for(int i=1;i<=100000;i++)
      p[i]=(p[i-1]*10)%mod;

}


int main()
{

 
  pre();


  string s;
   while(cin>>s)
  {
       reverse(s.begin(),s.end());

      inta tempans=0;
      for(int i=0;i<s.length();i++)
         tempans=(tempans+p[i]*(s[i]-'0'))%mod;

      if(tempans==0)  tempans=mod-1;
      else tempans-=1;

     cout<<quick_mod(2,tempans,modp)<<endl;


  }
}


原文地址:https://www.cnblogs.com/814jingqi/p/3280289.html