POJ 1286 Necklace of Beads(置换群)

55 篇文章 2 订阅
27 篇文章 0 订阅

oh my gosh 

一直time error的原因是因为定义longlong的n 结果输入%d

就一直time error ctmd。

置换群首先考虑旋转。 

然后再考虑翻转 翻转分为奇偶数来考虑总数。

最后除以置换的个数。

 /*
 qq:1239198605
 ctgu_yyf
        */

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;

int gcd(int a,int b)
{
	if(b==0)
	return a;
	else
	return gcd(b,a%b);
}

/*
//快速幂 
ll powx(ll a,ll b)
{
	ll  res=1;
	
	if(a==1)
	return a;
	
	while(b>0)
	{
		if(b&1)
		res=res*a;
		
		a=a*a;
		b=b>>1;
	}
	return res;
}
*/

ll powx(ll x,ll k) {
    if( k == 1 ) return x ;
    ll s = pow(x,k/2) ;
    s = s*s ;
    if( k%2 ) s *= x ;
    return s ;
}

int main()
{
  // ios::sync_with_stdio(false);
   
   
   ll n; 
   while(scanf("%lld",&n) && n != -1)
   {	
   	if(n==0)
   	{   
   	printf("0\n");
   	continue;}
   	
   	ll ans=0;
   	
   	//旋转置换 
   	for(int i=1;i<=n;i++)
   	ans=ans+powx(3,gcd(n,i));
   	
   	//翻转置换
   	//如果n为奇数 
	if(n%2)
	ans=ans+powx(3,n/2+1)*n;
	else
	{
		ans=ans+powx(3,n/2)*(n/2);
		ans=ans+powx(3,n/2+1)*(n/2);
	}
	
	//cout<<(ans/(n*2))<<endl;
	printf("%lld\n",(ans/(n*2)));
   } 


return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值