令$f(x) = x^{2^{k}-1}$,我们可以在$O(k)$的时间内求出$f(x)$。

如果对$1$到$n$都跑一遍这个求解过程,时间复杂度$O(kn)$,在规定时间内无法通过。

所以需要优化。

显然这是一个积性函数,那么实际上只要对$10^{6}$以内的质数跑$O(k)$的求解过程。

而$10^{6}$以内的质数不到$8*10^{4}$个,优化之后可以通过。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define MP make_pair
#define fi first
#define se second typedef long long LL; const int N = 1e6 + 10; int f[N];
int c[N];
int ans; int Pow(int i, int k, int m){
int p = i, q = p;
rep(j, 1, k - 1){
q = 1ll * q * q % m;
p = 1ll * p * q % m;
} return p;
} int cal(int x, int k, int m){
if (~f[x]) return f[x];
else return f[x] = Pow(x, k, m);
} class Powerit {
public:
int calc(int n, int k, int m){
memset(f, -1, sizeof f);
ans = 0;
rep(i, 1, 1e6){
for (int j = i + i; j <= 1e6; j += i) c[j] = i;
} ans = cal(1, k, m);
rep(i, 2, n){
int x = c[i], y = i / c[i];
if (x == 1){
ans = ans + (f[i] = cal(i, k, m));
ans %= m;
continue;
} f[i] = 1ll * cal(x, k, m) * cal(y, k, m) % m;
ans = ans + f[i];
ans %= m;
} return ans; }
};

  

TopCoder SRM 660 Div2 Problem 1000 Powerit (积性函数)的更多相关文章

  1. TopCoder SRM 301 Div2 Problem 1000 CorrectingParenthesization(区间DP)

    题意  给定一个长度为偶数的字符串.这个字符串由三种括号组成. 现在要把这个字符串修改为一个符合括号完全匹配的字符串,改变一个括号的代价为$1$,求最小总代价. 区间DP.令$dp[i][j]$为把子 ...

  2. TopCoder SRM 701 Div2 Problem 900 ThueMorseGame(博弈+预处理)

    题意  Alice和Bob在玩一个游戏,Alice先手. 每次一个人可以从一堆式子中拿走任意数量(不超过m)的式子. 取走最后一颗式子的人胜利. 当一个取完某一步的时候剩下的石子数量的二进制表示中1的 ...

  3. POJ_2480 Longge's problem【积性函数+欧拉函数的理解与应用】

    题目: Longge is good at mathematics and he likes to think about hard mathematical problems which will ...

  4. POJ 2480 Longge&#39;s problem 积性函数

    题目来源:id=2480" style="color:rgb(106,57,6); text-decoration:none">POJ 2480 Longge's ...

  5. Problem : 这个题如果不是签到题 Asm.Def就女装(积性函数dp

    https://oj.neu.edu.cn/problem/1460 思路:若n=(p1^a1)*(p2^a2)...(pn^an),则f(n,0)=a1*a2*...*an,显然f(n,0)是积性函 ...

  6. POJ 2480 Longge's problem (积性函数,欧拉函数)

    题意:求∑gcd(i,n),1<=i<=n思路:f(n)=∑gcd(i,n),1<=i<=n可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子.为什么呢?原因 ...

  7. poj 2480 Longge's problem 积性函数

    思路:首先给出几个结论: 1.gcd(a,b)是积性函数: 2.积性函数的和仍然是积性函数: 3.phi(a^b)=a^b-a^(b-1); 记 f(n)=∑gcd(i,n),n=p1^e1*p2^e ...

  8. poj 2480 Longge&#39;s problem 积性函数性质+欧拉函数

    题意: 求f(n)=∑gcd(i, N) 1<=i <=N. 分析: f(n)是积性的数论上有证明(f(n)=sigma{1<=i<=N} gcd(i,N) = sigma{d ...

  9. HDU 1452 Happy 2004(因数和+费马小定理+积性函数)

    Happy 2004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

随机推荐

  1. HDU:4417-Super Mario(离线线段树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  2. [BZOJ2947]促销(Splay)

    Description Great Bytelandish的超级市场网络请你编写一个程序模拟促销商品的成本费用(simulating costs of the promotionbeing prepa ...

  3. 开源中国app说什么 旁边的那个图标是什么drawable

    妈的,那就只是一个 ActionBarDrawerToggle: 配合侧滑菜单而生的而已.

  4. centos开机启动项设置命令:chkconfig

    在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd.mysqld.postfix等,安装后系统默认不会自动启动的.就算手动执行/etc/init.d/mysqld start ...

  5. Python-S9-Day114——Flask开始实战

    01 今日内容概要 02 课前分享 03 内容回顾 04 路飞学城表结构(一) 05 路飞学城表结构(二) 06 路飞学城立即支付思路 07 今日作业 08 初识Flask 09 werkzug 10 ...

  6. php字符串 函数

    strtolower()//字符串转化小写的字母 $str="abcdEfG";$s=strtolower($str); 输出:abcdefg; strtoupper();字符串转 ...

  7. CSU-2173 Use FFT

    CSU-2173 Use FFT Description Bobo computes the product P(x)⋅Q(x)=\(c_0 + c_1x + - + c_{n+m}x^{n + m} ...

  8. python 笔试总结

    1.对比两种函数对应结果 def fn(x): if x>0: print(x) fn(x-1) ****结果****** 3 2 1 $$$$$$另外一种$$$$$$$$$ def fn(x) ...

  9. [HTTPS]pfx转jks

    keytool -importkeystore -srckeystore  src.pfx -srcstoretype pkcs12 -destkeystore trg.jks -deststoret ...

  10. hdu 3714 Error Curves(三分)

    Error Curves Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...