• hdu 3923 Invoker polya 定理


    /*
    * hdu3923.c
    *
    * Created on: 2011-9-20
    * Author: bjfuwangzhu
    */

    #include<stdio.h>
    #include<math.h>
    #define LL long long
    #define nnum 1000000007
    LL x, y;
    LL modular_exp(int a, int b) {
    LL res, temp;
    res = 1 % nnum,temp = a % nnum;
    while (b) {
    if (b & 1) {
    res = res * temp % nnum;
    }
    temp = temp * temp % nnum;
    b >>= 1;
    }
    return res;
    }
    int getPhi(int n) {
    int phi, i, te;
    te = (int) (sqrt(n * 1.0));
    for (i = 2, phi = n; i <= te; i++) {
    if (n % i == 0) {
    phi = phi / i * (i - 1);
    while (n % i == 0) {
    n /= i;
    }
    }
    }
    if (n > 1) {
    phi = phi / n * (n - 1);
    }
    return phi;
    }
    void extend_gcd(int a, int b) {
    if (b == 0) {
    x = 1LL, y = 0;
    return;
    }
    extend_gcd(b, a % b);
    LL tx = x;
    x = y, y = tx - a / b * y;
    }
    LL polya(int n, int m) {
    int i;
    LL sum;
    for (i = 1, sum = 0; i <= m; i++) {
    if (m % i == 0) {
    sum += modular_exp(n, i) * getPhi(m / i) % nnum;
    sum %= nnum;
    }
    }
    if (m & 1) {
    sum += modular_exp(n, (m + 1) / 2) * m % nnum;
    sum %= nnum;
    } else {
    sum += modular_exp(n, m / 2) * (m / 2) % nnum;
    sum %= nnum;
    sum += modular_exp(n, m / 2 + 1) * (m / 2) % nnum;
    sum %= nnum;
    }
    extend_gcd(m << 1, nnum);
    x = (x % nnum + nnum) % nnum;
    sum = sum * x % nnum;
    return sum;
    }
    int main() {
    #ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
    #endif
    int i, t, n, m;
    LL sum;
    while (~scanf("%d", &t)) {
    for (i = 1; i <= t; i++) {
    scanf("%d %d", &n, &m);
    sum = polya(n, m);
    printf("Case #%d: %I64d\n", i, sum);
    }
    }
    return 0;
    }

  • 相关阅读:
    BZOJ 1597: [Usaco2008 Mar]土地购买
    BZOJ 1005: [HNOI2008]明明的烦恼
    BZOJ 1004: [HNOI2008]Cards
    Burnside引理和Polya定理
    BZOJ 1003: [ZJOI2006]物流运输
    BZOJ 1002: [FJOI2007]轮状病毒
    BZOJ 1001: [BeiJing2006]狼抓兔子
    网络流 最大流dinic算法解释
    51nod 1299 监狱逃离
    2017.11.26【清华集训2017】模拟
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/2208608.html
Copyright © 2020-2023  润新知