Polya 定理练习题


《算法竞赛入门经典:训练指南》上的例题。

顺便做了 UVa11762:
P143 , 期望 Dp

Uva10294,11077,LA3641:
P146 ~ 149 , Polya 定理


Uva11762

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

const int maxn = 1e6+4;

int prime[maxn], tot;
bool check[maxn];
double f[maxn];
bool vis[maxn];

void PreWork()
{
    for(int i = 2; i < maxn; i++)
    {
        if(!check[i]) prime[++tot] = i;

        for(int j = 1; j <= tot && i*prime[j] < maxn; j++)
        {
            check[i*prime[j]] = true;

            if(!(i%prime[j])) break;    
        }
    }
}

double dp(int x)
{
    if(x == 1) return 0;
    if(vis[x]) return f[x];

    vis[x] = true;

    double &ans = f[x];

    int g = 0, p = 0;
    ans = 0;

    for(p = 1; p <= tot && prime[p] <= x; p++)
        if(!(x%prime[p])) ans += dp(x/prime[p]), g++;   

    ans = (ans + (--p))/g;
    return ans;
}

int main()
{
    int T;
#ifndef ONLINE_JUDGE    
    freopen("uva11762.in","r",stdin);
    freopen("uva11762.out","w",stdout);
#endif

    PreWork(), std::cin >> T;

    for(int i = 1, q; i <= T; i++)
        scanf("%d",&q), printf("Case %d: %.10lf\n", i, dp(q));

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;       
}

LA3641

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>


void Solve()
{
    char str[30] = {'\0'};
    bool hash[30] = {false}; 
    int cnt[30] = {0};
    bool flag = true;

    std::cin >> str;

    for(int i = 0; i < 26; i++)
        if(!hash[i])
        {
            int len = 0;
            for(int j = i; !hash[j]; len++)
                hash[j] = true, j = str[j]-'A';
            cnt[len]++;
        }

    for(int i = 2; i <= 26; i += 2)
        if(cnt[i]&1) {flag = false; break;}

    if(flag) std::cout << "Yes" << std::endl;
    else std::cout << "No" << std::endl;        
}

int main()
{
    int T;
#ifndef ONLINE_JUDGE
    freopen("LA3641.in","r",stdin);
    freopen("LA3641.out","w",stdout);   
#endif

    std::cin >> T;

    while(T--) Solve();

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;       
}

UVa10294

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>

int n, t;

long long power(int x,int y)
{
    long long ret = 1;
    while(y--) ret *= x;
    return ret;
}
int gcd(int x,int y)
{
    if(!y) return x;
    return gcd(y,x%y); 
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("uva10294.in","r",stdin);
    freopen("uva10294.out","w",stdout); 
#endif

    while(~scanf("%d",&n))
    {
        long long a = 0, b = 0;

        std::cin >> t;

        for(int i = 0; i < n; i++) a += power(t,gcd(i,n));

        if(n&1) b = n *power(t,(n+1)>>1);
        else  b = (n>>1)*(power(t, (n>>1)+1) + power(t, n>>1));

        std::cout << a/n << ' ' << (((a+b)/n)>>1) << std::endl;     
    }


#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;       
}

UVa11077

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
const int maxn = 30;

int n, k;
unsigned long long f[maxn][maxn];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("uva11077.in","r",stdin);
    freopen("uva11077.out","w",stdout); 
#endif

    f[1][0] = 1;

    for(int i = 2; i <= 21; i++)
        for(int j = 0; j < i; j++)
            f[i][j] = f[i-1][j] + ((j)?f[i-1][j-1]*(i-1):0);

    while(true)
    {
        std::cin >> n >> k;

        if(!(n+k))break;

        std::cout << f[n][k] << std::endl;
    }


#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值