ACM 第十八天

数学基础(卷积,FFT,FWT,FMT,鸽巢原理,群论,哈里亚余数,哈里亚计数定理,组合数学,LVG定理,期望DP,期望点贡献问题)

 练习题:

A - Necklace of Beads

Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 24 ). If the repetitions that are produced by rotation around the center of the circular necklace or reflection to the axis of symmetry are all neglected, how many different forms of the necklace are there?

Input
The input has several lines, and each line contains the input data n.
-1 denotes the end of the input file.
Output
The output should contain the output data: Number of different forms, in each line correspondent to the input data.
Sample Input
4
5
-1
Sample Output
21
39
 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<algorithm>
 4 #include <iostream>
 5 //#define long long ll
 6 using namespace std;
 7 
 8 int gcd(int x,int y)
 9 {
10     if(x%y==0) return y;
11     else return (gcd(y,x%y));
12 }
13 int main()
14 {
15     int n;
16     long long  ans;
17     while(~scanf("%d",&n) && n!=-1)
18     {
19          if(n<=0) //注意n没有最小值范围哦
20         {
21             printf("0\n");
22             continue;
23         }
24         ans=0;
25         for(int i=1; i<=n; i++)
26             ans+=pow(3, gcd(i,n));//旋转
27         if(n%2) //翻转
28         {
29             ans+=n*pow(3,n/2+1);
30         }
31         else
32         {
33             ans+=(n/2)*pow(3,n/2);
34             ans+=(n/2)*pow(3,n/2+1);
35         }
36         ans=ans/(2*n);
37         printf("%lld\n",ans);
38     }
39     return 0;
40 }

 

C - Thief in a Shop

A thief made his way to a shop.

As usual he has his lucky knapsack with him. The knapsack can contain k objects. There are n kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind i is ai.

The thief is greedy, so he will take exactly k products (it's possible for some kinds to take several products of that kind).

Find all the possible total costs of products the thief can nick into his knapsack.


Input

The first line contains two integers n and k (1 ≤ n, k ≤ 1000) — the number of kinds of products and the number of products the thief will take.

The second line contains n integers ai (1 ≤ ai ≤ 1000) — the costs of products for kinds from 1 to n.

Output

Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order.

Examples
Input
3 2
1 2 3
Output
2 3 4 5 6
Input
5 5
1 1 1 1 1
Output
5
Input
3 3
3 5 11
Output
9 11 13 15 17 19 21 25 27 33
 1 #include<stdio.h>
 2 #include<math.h>
 3 #include<algorithm>
 4 #include <iostream>
 5 #include<queue>
 6 using namespace std;
 7 const int maxn=1e3+5;
 8 
 9 int a[maxn],dp[maxn*maxn];
10 
11 int main()
12 {
13     int n,k;
14     cin>>n>>k;
15     for(int i=1;i<=n;i++)
16     {
17         cin>>a[i];
18     }
19     sort(a+1,a+n+1);
20     n=unique(a+1,a+n+1)-(a+1);
21     for(int i=2;i<=n;i++)
22     {
23         a[i]=a[i]-a[1];
24     }
25     for(int i=1;i<=k*a[n];i++)
26     {
27         dp[i]=k+1;
28     }
29     for(int i=2;i<=n;i++)
30     {
31         for(int j=a[i];j<=k*a[i];j++)
32         {
33             dp[j]=min(dp[j],dp[j-a[i]]+1);
34         }
35     }
36     for(int i=0;i<=k*a[n];i++)
37     {
38         if(dp[i]<=k)
39         {
40             cout<<k*a[1]+i<<" ";
41         }
42     }
43     cout<<endl;
44     return 0;
45 
46 }

 

 

 

posted @ 2018-08-23 13:03  白雪儿  Views(102)  Comments(0Edit  收藏  举报