Mean: php
給定一個大整數N,求1到N中每一個數的因式分解個數的總和。ios
analyse:ide
N可達10^100000,只能用數學方法來作。優化
首先想到的是找規律。經過枚舉小數據來找規律,發現其實answer=pow(2,n-1);ui
分析到這問題就簡單了。因爲n很是大,因此這裏要用到費馬小定理:a^n ≡ a^(n%(m-1)) * a^(m-1)≡ a^(n%(m-1)) (mod m) 來優化一下,否則直接用快速冪會爆。this
Time complexity: O(n)spa
Source code: code
/* * this code is made by crazyacking * Verdict: Accepted * Submission Date: 2015-05-22-21.21 * Time: 0MS * Memory: 137KB */ #include <queue> #include <cstdio> #include <set> #include <string> #include <stack> #include <cmath> #include <climits> #include <map> #include <cstdlib> #include <iostream> #include <vector> #include <algorithm> #include <cstring> #define LL long long #define ULL unsigned long long using namespace std; const int mod=1e9+7; const int MAXN=100010; char s[MAXN]; long long quickPower(long long a,long long b,long long m) { long long ans=1; while(b) { if(b&1) ans=(ans*a)%m,b--; b/=2,a=a*a%m; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); while(~scanf("%s",s)) { ULL n=0; for(int i=0;s[i];++i) n=(n*10+s[i]-'0')%(mod-1); printf("%d\n",(int)quickPower(2,((n-1)%(mod-1))%mod,mod)); } return 0; } /* */