Sum

开始的时候死活时没有看懂题目是什么意思,光是题意就看了半个多小时,还好最后看懂了发火

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

其实就是用到了小费马定理a^(p-1) ≡ 1 (mod p)

#include <iostream>  
#include <cstdio>   
#include <cstring>   
using namespace std; 

__int64 pow(__int64 b){
	__int64 a=2;
	__int64 ans=1;
	while(b>0){
		if(b&1)   //判断是否为奇数,相当于 if(b%2==1)
	    	ans=(ans*a)%1000000007;
		a=(a*a)%1000000007;
		b=b>>1;   //二进制向右移一位,相当于 b=b/2;
	}
	return ans;
}

int main ()
{
	char str[100100]; 
  while(~scanf("%s",str))
  {
	  __int64 sum=0;//str[0]-'0';
       int len=strlen(str);//注意这里如果是没有Len的话就会超时
   for(int i=0; i<len; i++)
      sum=(sum*10+str[i]-'0') % 1000000006;//小费马定理 

    printf("%I64d
",pow(sum-1));
  }         
	return 0;   
} 


 

原文地址:https://www.cnblogs.com/zswbky/p/5432036.html