标签:前缀 names end space == clu ios div stream
题意:
称一对字符串(A,B)是相似的,当且仅当满足以下条件:
#include <iostream> #include <cstdio> #include <map> #include <vector> #include <algorithm> using namespace std; typedef long long LL; const LL MOD = 1e9 + 7; map<int, LL> dp; vector<LL> G; int n, k; LL mypow(LL a, LL b) { LL ans = 1; for(; b; b >>= 1, (a *= a) %= MOD) if(b&1) (ans *= a) %= MOD; return ans; } LL dfs(LL n){ if(dp[n]) return dp[n]; dp[n] = mypow(k, n); for(auto x : G) { if(x >= n) break; if(n % x == 0) (dp[n] = dp[n] - dfs(x) + MOD) %= MOD; } return dp[n]; } int main() { cin>>n>>k; for(int i = 1; i*i <= n; i++) if(n % i == 0) { G.push_back(i); if(n/i != i) G.push_back(n/i); } sort(G.begin(), G.end()); dfs(n); long long ans = 0; for(auto x : G) (ans += (LL)x*dp[x]) %= MOD; cout<<ans<<endl; return 0; }
标签:前缀 names end space == clu ios div stream
原文地址:http://www.cnblogs.com/Saurus/p/7647164.html