zoukankan      html  css  js  c++  java
  • SCOJ 4423: Necklace polya

    4423: Necklace

    题目连接:

    http://acm.scu.edu.cn/soj/problem.action?id=4423

    Description

    baihacker bought a necklace for his wife on their wedding anniversary.
    A necklace with N pearls can be treated as a circle with N points where the
    distance between any two adjacent points is the same. His wife wants to color
    every point, but there are at most 2 kinds of color. How many different ways
    to color the necklace. Two ways are said to be the same iff we rotate one
    and obtain the other.

    Input

    The first line is an integer T that stands for the number of test cases.
    Then T line follow and each line is a test case consisted of an integer N.

    Constraints:
    T is in the range of [0, 4000]
    N is in the range of [1, 1000000000]
    N is in the range of [1, 1000000], for at least 75% cases.

    Output

    For each case output the answer modulo 1000000007 in a single line.

    Sample Input

    6
    1
    2
    3
    4
    5
    20

    Sample Output

    2
    3
    4
    6
    8
    52488

    Hint

    题意

    有一个长度为n的环,环上每个点的颜色有两种,然后你可以旋转

    问你本质不同的串有多少种

    题解:

    ploya裸题

    答案ans[i] = 1/n sigma(d|n)phi(d)*2^(n/d)

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int mod = 1e9+7;
    long long quickpow(long long  m,long long n,long long k)//返回m^n%k
    {
        long long b = 1;
        while (n > 0)
        {
              if (n & 1)
                 b = (b*m)%k;
              n = n >> 1 ;
              m = (m*m)%k;
        }
        return b;
    }
    long long phi(long long n)
    {
        long long tmp=n;
        for(long long i=2;i*i<=n;i++)
            if(n%i==0)
            {
                tmp/=i;tmp*=i-1;
                while(n%i==0)n/=i;
            }
        if(n!=1)tmp/=n,tmp*=n-1;
        return tmp;
    }
    void solve()
    {
        int n;
        scanf("%d",&n);
        long long ans = 0;
        for(int i=1;i*i<=n;i++)
        {
            if(n%i==0)
            {
                ans = ans + phi(i)*quickpow(2,(n/i),mod)%mod*quickpow(n,mod-2,mod)%mod;
                ans%= mod;
                if(i!=n/i)
                {
                    ans = ans + phi(n/i)*quickpow(2,i,mod)%mod*quickpow(n,mod-2,mod)%mod;
                    ans%= mod;
                }
            }
        }
        cout<<ans<<endl;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)solve();
    }
  • 相关阅读:
    前端工程师须知pc电脑端分辨率
    移动前端的坑
    07.01工作笔记
    缓存
    word-wrap,white-space和text-overflow属性
    页面结构
    Spring Bean的作用域和自动装配
    Spring配置文件
    初识Spring和IOC理解
    MyBatis缓存
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5398830.html
Copyright © 2011-2022 走看看