魔法手链(burnside+矩阵优化+dp acwing 3134)

题目:3134. 魔法手链 - AcWing题库

思路:

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
using namespace std;
#define  LL long long 
const int N = 2e5+ 10;
const int M = 200000 + 10;
const int P = 1331;
const long long mod = 9973;
const double PI = acos(-1);
int n, m, k;
struct matrix
{
    int a[11][11];
    matrix() {
        memset(a, 0, sizeof a);
    }
}t;
LL phi(LL x)
{     
    LL ans=x;
    for (int i = 2; i * i <= x; i++)
    {
        if (x % i == 0)
        {
            ans = ans/ i*(i-1);
            while (x % i == 0)
                x /= i;
        }
    }
    if (x > 1)
        ans = ans/x*(x-1);
    return ans;
}
struct matrix mul(struct matrix g, struct matrix h)
{
    struct matrix c;
    for(int i=0;i<m;i++)
        for (int j = 0; j < m; j++)
        {
            c.a[i][j] = 0;
            for (int k = 0; k < m; k++)
            {   
                c.a[i][j] = (c.a[i][j] + g.a[i][k] * h.a[k][j] % mod) % mod;
            }
        }
    return c;
}
LL f(int b)
{
    struct matrix  r=t,ans;
    for (int i = 0; i < m; i++) ans.a[i][i] = 1;
    while (b)
    {        
        if (b & 1)
            ans = mul(ans, r);
        b >>= 1;
        r = mul(r, r);
    }
    LL res = 0;
    for (int i = 0; i < m; i++)
        res = (res+ans.a[i][i])%mod;
    return res;
}
LL quick(LL a, LL b,LL mod)
{
    LL ans = 1;
    while (b)
    {
        if (b & 1) ans = ans * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return ans;
}
int main() {
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> m >> k;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < m; j++)
                t.a[i][j] = 1;
        for (int i = 1; i <= k; i++)
        {
            int x, y;
            cin >> x >> y;
            x--;
            y--;
            t.a[x][y] = 0;
            t.a[y][x] = 0;
           }
        LL ans = 0;
        for (LL i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {     
                ans = (ans + (LL)phi(n / i) * f(i) % mod) % mod;
                if (i * i != n) ans = (ans + (LL)phi(i) * f(n / i) % mod) % mod;
             }
        }
        cout << ans*quick(n,mod-2,mod)%mod << endl;
    }
    return 0;
}  

  • 24
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值