HDU 1452 Happy 2004(积性函数 逆元)
2015-09-07 16:12
204 查看
数学题就是99%的时间在纸上1%的时间写代码。
Orz...
设sum(x)为x的因子和,由于sum(x)为积性函数,从而有sum(a*b) = sum(a) * sum(b),其中a和b互素。
故:
2004 = 2^2 x 3 x 167 => 2004^x = 2^(2x) * 3^x * 167 ^x => sum = (2^(2x) - 1) * (3^x - 1) / 2 * (167^x - 1) / 166 =(2^(2x)
- 1) * (3^x - 1) / 2 * ((167%29)^x - 1) / (166%29) = (2^(2x) - 1) * (3^x - 1) / 2 * (22^x - 1) / 21.
(a * b) / c % MOD = (a % MOD) * (b % MOD) * inv(c).
inv(c)为 c * t = 1 % MOD c模MOD的逆元。
只有两个数,很容易算出inv(2) = 15, inv(21) = 22.
关于逆元的求解,详细的可以看:/article/2371887.html
所以sum(2004^x) = (2^(2x) - 1) * (3^x - 1) * 15 * (167^x - 1) * 18.
Orz...
于是主要的代码就几行了。
代码:
Orz...
设sum(x)为x的因子和,由于sum(x)为积性函数,从而有sum(a*b) = sum(a) * sum(b),其中a和b互素。
故:
2004 = 2^2 x 3 x 167 => 2004^x = 2^(2x) * 3^x * 167 ^x => sum = (2^(2x) - 1) * (3^x - 1) / 2 * (167^x - 1) / 166 =(2^(2x)
- 1) * (3^x - 1) / 2 * ((167%29)^x - 1) / (166%29) = (2^(2x) - 1) * (3^x - 1) / 2 * (22^x - 1) / 21.
(a * b) / c % MOD = (a % MOD) * (b % MOD) * inv(c).
inv(c)为 c * t = 1 % MOD c模MOD的逆元。
只有两个数,很容易算出inv(2) = 15, inv(21) = 22.
关于逆元的求解,详细的可以看:/article/2371887.html
所以sum(2004^x) = (2^(2x) - 1) * (3^x - 1) * 15 * (167^x - 1) * 18.
Orz...
于是主要的代码就几行了。
代码:
// Header. #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cstdio> #include <vector> #include <string> #include <bitset> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <set> #include <map> using namespace std; // Macro typedef long long LL; #define TIME cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s." << endl; #define IN freopen("/Users/apple/input.txt", "r", stdin); #define OUT freopen("/Users/apple/out.txt", "w", stdout); #define mem(a, n) memset(a, n, sizeof(a)) #define rep(i, n) for(int i = 0; i < (n); i ++) #define REP(i, t, n) for(int i = (t); i < (n); i ++) #define FOR(i, t, n) for(int i = (t); i <= (n); i ++) #define ALL(v) v.begin(), v.end() #define Min(a, b) a = min(a, b) #define Max(a, b) a = max(a, b) #define put(a) printf("%d\n", a) #define ss(a) scanf("%s", a) #define si(a) scanf("%d", &a) #define sii(a, b) scanf("%d%d", &a, &b) #define siii(a, b, c) scanf("%d%d%d", &a, &b, &c) #define VI vector<int> #define pb push_back const int inf = 0x3f3f3f3f, N = 1e2 + 5, MOD = 29; // Macro end int T, cas = 0; int n, m; // Imp int Pow(int a, int b) { int ret = 1; while(b) { if(b & 1) ret = (ret * a) % MOD; a = (a * 2) % MOD; b >>= 1; } return ret % MOD; } int main(){ #ifdef LOCAL IN // OUT #endif while(si(n) != EOF, n) { int a = Pow(2, 2 * n + 1) - 1, b = Pow(3, n + 1) - 1, c = Pow(22, n + 1) - 1; int t = a * b * c * 15 * 18; cout << a * b * c * 15 * 18 % MOD << endl; } return 0; }
相关文章推荐
- 黑马程序员——Objective-C Foundation框架中的NSMutableString对象
- Android Afinal使用与总结
- appium python api
- Android Activity生命周期详解
- android:ellipsize="end" 失效或者 相关的Bug
- HTML5移动端数据图表组件调研
- 黑马程序员——Objective-C Foundation框架中的NSString对象
- Android笔记(二十二) Android中的GridView
- [置顶] 微信开放平台的第三方平台、全网发布流程、组件API、返回普通文本消息
- Android studio dabao
- iOS开发 - 证书失效
- Unity 2D游戏开发教程之为游戏场景添加多个地面
- Android系统自带样式(android:theme)详解
- IOS CoreText --- 图文混排
- android studio 编程中用到的快捷键
- Android应用Design Support Library完全使用实例
- iOS运行时应用场合二 获取对象属性
- android studio下导入.so库
- iOS应用崩溃日志分析
- Android Studio系列-签名打包