zoukankan      html  css  js  c++  java
  • LightOJ 1419 – Necklace Polya计数+费马小定理求逆元

    **题意:**给你n个珠子可以染成k种颜色,旋转后相同的视为一种,问共有几种情况 **思路:**开始按照一般的排列组合做发现情况太多且要太多运算,查了下发现此题是组合中Polya定理模板题… 学的浅只能大致一说公式**Sigma(k^gcd(i-1,n))/n**求和数量取决于置换群数量,由于这个成环共有n个置换群,而GCD是求当前置换群的等价置换的数量。 注意由于最后要除n,如果直接取模会出现问题。通过费马小定理求得乘法逆元为pow(n,p-2)%p; 其中p为质数。
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <utility>
    #include <vector>
    #include <map>
    #include <set>
    #include <string>
    #include <stack>
    #include <queue>
    #define LL long long
    #define MMF(x) memset((x),0,sizeof(x))
    #define MMI(x) memset((x), INF, sizeof(x))
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int N = 1e5+2000;
    const LL mod = 1000000007;
    LL gcd(LL a, LL b)
    {
    return b==0?a:gcd(b, a%b);
    }
    LL poww(LL a, LL n)
    {
    LL r = 1;
    while(n)
    {
    if(n & 1)
    {
    r = (r * a) % mod;
    }
    n >>= 1;
    a = (a * a) % mod;
    }
    return r % mod;
    }

    int main()
    {

    int T;
    int cnt = 0;
    cin >> T;
    while(T--)
    {
    LL n, k, sum = 0;
    scanf("%lld%lld", &n, &k);
    for(LL i = 0; i < n; i++)
    {
    LL t = gcd(n, i);
    sum = (poww(k, t) + sum ) % mod;
    }
    printf("Case %d: %lld ", ++cnt,(sum*poww(n, mod-2))%mod);//费马小定理求逆元
    }
    return 0;
    }

  • 相关阅读:
    调用AsyncTask的excute方法不能立即执行程序的原因分析及改善方案
    辅助
    目录检索
    高斯消元法
    树套树
    珂朵莉树
    卢卡斯定理
    中国剩余定理
    数论基础
    网络流基础
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/6008120.html
Copyright © 2011-2022 走看看