UESTC 75 The Queen's New Necklaces

题意:一个项链的珠子的颜色有若干种。每种颜色的珠子个数为Ai。求有多少种不同的项链?

我们考虑,如果旋转i个珠子,那么会产生gcd(n,i)个循环节,每个循环节的大小我们假设为K,那么如果有一个颜色的数量不是K的倍数,那么必然没有置换过后等价的情况,然而,如果全部都是K的倍数,那么我们就相当于在gcd(n,i)个空位里面,每种颜色放入a[i]/K个的方案总数,这是一个简单的组合计数问题,方法大概就是c(n,m1)*c(n-m1,m2)*c(n-m1-m2,m3)*... ...,然后还要再除以n,由于这题n高达1000,需要高精度,但是我比较懒,就没有打高精度的版本了。

 1 #include<algorithm>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iostream>
 6 int a[200005],n,m,p[200005];
 7 int gcd(int a,int b){
 8     if (b==0) return a;
 9     else return gcd(b,a%b);
10 }
11 int C(int n,int m){
12     int res=1;
13     for (int i=1;i<=n;i++) res*=i;
14     for (int i=1;i<=n-m;i++) res/=i;
15     for (int i=1;i<=m;i++) res/=i;
16     return res;
17 }
18 int main(){
19     int T,ans;
20     scanf("%d",&T);
21     while (T--){
22         scanf("%d",&m);n=0;
23         for (int i=1;i<=m;i++) {scanf("%d",&a[i]);n+=a[i];}
24         int ans=0;
25         for (int i=0;i<n;i++){
26             int num=gcd(i,n);
27             int K=n/num,tot=0;
28             p[0]=0;
29             bool pd=1;
30             for (int j=1;j<=m&&pd;j++){
31                 if (a[j]%K){
32                     pd=0;
33                 }else{
34                     p[++p[0]]=a[j]/K;
35                     tot+=a[j]/K;
36                 }
37             }
38             if (!pd) continue;
39             for (int j=1;j<=p[0];j++)
40              ans+=C(tot,p[j]),tot-=p[j];
41         }
42         printf("%d\n",ans/n);
43     }
44 }

UESTC 75 The Queen's New Necklaces

时间: 2024-08-07 03:45:27

UESTC 75 The Queen's New Necklaces的相关文章

2016 UESTC Training for Dynamic Programming

2016 UESTC Training for Dynamic Programming A - 柱爷与咸鱼神功 题意: 柱爷有n(<=5000)点心情去学m(<=5000)个招式,每个招式会得到一定的修炼值,但要消耗一定的心情,求最多的修炼值. 题解: 0.这是一个裸的背包问题,用每一个物品去更新每一种背包的状态. 1.状态定义:dp[i]表示用i点心情得到的最多修炼值. 2.状态转移:dp[i] = max{dp[i-v[j]]+w[j]} 代码: 1 2 3 4 5 6 7 8 9 10

UESTC 2014 Summer Training #3 Div.2

(更新中) A:ZOJ 3611 BFS+状态压缩 [题意]:给定一张n*m的图,图上每个点有如下情况:L,R,D,U:代表在该点上只能往它已经给定的方向前进.#,W:不能走到该点.$:走到该点,可以花两分钟得到一分值,然后可以从该点向任意方向走.0:走到该点后可以向任意方向走.然后给你起点和终点坐标,问是否能从起点走到终点,如果能,求出可获得的最大分值以及与之对应达到该最大分值所需的的最小时间花 费.(其中起点和终点坐标可以相同)[知识点]:BFS+状态压缩[题解]:我觉得超级棒的题!真心感觉

UESTC 360(1425) another LCIS

这道题是CD老OJ上面的一道题,现在在新OJ上的题号是360,开始在VJ上做的提交一直RE(囧).后来才知道OJ移位了. 这道题是一个简单的成段更新+区间合并的线段树的题,1A还让我小激动了一下 这道题的大概意思是有两种操作,一种是成段地增加一个值,另外一种是询问从l到r这段区间内的最长递增子序列 首先先分析一下,如果某一段的值成段地增加一个量,那么该区间内的数的相对大小是不变的,因此递增子序列的长度是不会改变的 是要分析对于结果有影响的信息与值:一是每个子区间中的最值,二是有可能在两个区间合并

UESTC 360 Another LCIS

Another LCIS Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on UESTC. Original ID: 142564-bit integer IO format: %lld      Java class name: Main For a sequence S1,S2,...,SN, and a pair of integers (i, j), if 1 <= i <= j <= N

UESTC 电子科大专题训练 数据结构 D

UESTC 1584 题意:平面坐标上有n个怪物,每个怪物有一个rank值,代表x坐标和y坐标都不大于它本身的怪物数(不包括本身) 思路:对x y坐标从小到大排序,x优先排序,用数状数组计算y坐标小于它的数量 AC代码: #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #i

uva 11195 Another queen (用状态压缩解决N后问题)

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2136 Problem A Another n-Queen Problem I guess the n-queen problem is known by every person who has studied backtracking. In this problem you s

UESTC 31 饭卡(Card) --背包问题

背包问题. 思路:如果m<5,此时也不能消费,所以此时答案为m m>=5: 求出背包容量为m-5,买前n-1样便宜的菜(排个序)的最大价值(即最大消费,即消费完后剩余值最接近5)最后减去最大的那个菜的价格,就得到最小的余额. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using n

UESTC 电子科大专题训练 数据结构 A

UESTC 1591 题意:求区间极值之差 思路:线段树裸题,不带更新 ACA代码: #include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "ma

2017 UESTC Training for Data Structures

2017 UESTC Training for Data Structures A    水,找区间极差,RMQ怼上去. #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a;i<=b;i++) #define per(i,b,a) for (int i=b;i&