Polya 定理练习题 分类: uva 2015-06-17 22:13 28人阅读 评论(0) 收藏
《算法竞赛入门经典:训练指南》上的例题。
顺便做了
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
}
版权声明:本文为博主原创文章,未经博主允许不得转载。