zoukankan      html  css  js  c++  java
  • HDU 6264 (深搜,数论)

    题目链接

    题意

    (sum_{d|n}phi (d) imes {nover d}),其中(phi(n) = nprod_{p|n}({1-{1over p}}))

    分析

    (phi(d)) 分解式子代入可知:(sum_{d|n}(n imes prod_{p|d}(1-{1over 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
    ",ans);
        }
        return 0;
    }
    

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

  • 相关阅读:
    2015第11周日
    2015第11周六
    素数推断算法(高效率)
    vc中关于 directx的配置,和dxsdk_extras(directshow)
    几种开源分词工具的比較
    javascript实现函数的默认參数值方法
    MyReport报表引擎2.0.0.0新功能
    怎样在小方框上打对号 小方框内打对勾 word 方框打对勾
    Bombing HDU, 4022(QQ糖的消法)
    fullcalendar日历控件知识点集合
  • 原文地址:https://www.cnblogs.com/1625--H/p/11545215.html
Copyright © 2011-2022 走看看