码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 6264 (深搜,数论)

时间:2019-09-18 20:51:01      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:code   c++   题目   include   提醒   sum   math   pre   ++   

题目链接

题意

\(\sum_{d|n}\phi (d) \times {n\over d}\),其中\(\phi(n) = n\prod_{p|n}({1-{1\over p}})\)

分析

\(\phi(d)\) 分解式子代入可知:\(\sum_{d|n}(n\times \prod_{p|d}(1-{1\over p}))\)
\(d\)\(n\) 的因子,枚举 \(d\) 的质因子的所有可能的组成情况共\(2^c\)中。 其中 c 为 n 的不同质因子个数(即题目中输入的 n )。
对于每种组成情况,例如\(d\) 的质因子为\(p_1,p_2,\cdots p_m\) ,我们枚举的是所有 p 的组成情况,而 每个 p 的指数都会影响 d 的实际大小。到这里,了解过如何计算一个数的因子个数的朋友一定知道如何解决该题目了。我们只需要计算满足这个质因子组成的 d 的个数就可以计算了

变量说明

  • ab[i] : 即 \(a[i] ^ {b[i]}\)
  • ab2[i] : 即 \(a[i] ^ {b[i] - 1}* (a[i]-1)\)
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int T,a[22],b[22],ab[22],ab2[22],n;
int ksm(int a,int b){
    int res = 1;
    for(;b;b>>=1){
        if(b&1)res = 1ll * res * a % mod;
        a = 1ll * a * a % mod;
    }
    return res;
}
int ans = 0;
// now 为 大小 ,num 为 个数
void dfs(int x,int now,int num){
    if(x > n){
        ans = (ans + 1ll * now * num % mod) % mod;
        return ;
    }
    //如果不选第 x 个质因子
    dfs(x+1,1ll * ab[x] * now % mod, num);
    //如果选择第 x 个质因子
    dfs(x+1,1ll * ab2[x] * now % mod,1ll * num * b[x] % mod);
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&a[i],&b[i]);
            ab[i] = ksm(a[i],b[i]);
            ab2[i] = ksm(a[i],b[i] - 1);
            ab2[i] = 1ll * ab2[i] * (a[i] - 1) % mod;
        }
        ans = 0;
        dfs(1,1,1);
        printf("%d\n",ans);
    }
    return 0;
}

特别提醒:用状压来表示所有选择情况的朋友可能会得到TLE的惊喜

HDU 6264 (深搜,数论)

标签:code   c++   题目   include   提醒   sum   math   pre   ++   

原文地址:https://www.cnblogs.com/1625--H/p/11545215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!