1091. Tmutarakan Exams

1091. Tmutarakan Exams

Time limit: 1.0 second
Memory limit: 64 MB

University of New Tmutarakan trains the first-class specialists in mental arithmetic. To enter the University you should master arithmetic perfectly. One of the entrance exams at the Divisibility Department is the following. Examinees are asked to find K different numbers that have a common divisor greater than 1. All numbers in each set should not exceed a given number S. The numbers K and S are announced at the beginning of the exam. To exclude copying (the Department is the most prestigious in the town!) each set of numbers is credited only once (to the person who submitted it first).

Last year these numbers were K=25 and S=49 and, unfortunately, nobody passed the exam. Moreover, it was proved later by the best minds of the Department that there do not exist sets of numbers with the required properties. To avoid embarrassment this year, the dean asked for your help. You should find the number of sets of K different numbers, each of the numbers not exceeding S, which have a common divisor greater than 1. Of course, the number of such sets equals the maximal possible number of new students of the Department.

Input

The input contains numbers K and S (2 ≤ K ≤ S ≤ 50).

Output

You should output the maximal possible number of the Department‘s new students if this number does not exceed 10000 which is the maximal capacity of the Department, otherwise you should output 10000.

Sample

input output
3 10
11

Problem Author: Stanislav Vasilyev
Problem Source: USU Open Collegiate Programming Contest March‘2001 Senior Session

Tags: number theory  (hide tags for unsolved problems)

Difficulty: 480    Printable version    Submit solution    Discussion (29)
My submissions    All submissions (8523)    All accepted submissions (2607)   Solutions rating (2071)

© 2000–2016 Timus Online Judge Team. All rights reserved.

思路:和http://www.cnblogs.com/zzuli2sjy/p/5467008.html一样;

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<iostream>
  4 #include<algorithm>
  5 #include<queue>
  6 using namespace std;
  7 typedef long long LL;
  8 bool prime[100];
  9 int ans[100];
 10 int  coutt[10000];
 11 LL dp[60][60];
 12 int ask[100];
 13 int id[100];
 14 queue<int>que;
 15 int main(void)
 16 {
 17         int i,j,k,p,q;
 18         dp[0][0]=1;
 19         dp[1][0]=1;
 20         dp[1][1]=1;
 21         for(i=2; i<=60; i++)
 22         {
 23                 for(j=0; j<=60; j++)
 24                 {
 25                         if(j==0||i==j)
 26                         {
 27                                 dp[i][j]=1;
 28                         }
 29                         else dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
 30                 }
 31         }
 32         for(i=2; i<=10; i++)
 33         {
 34                 if(!prime[i])
 35                 {
 36                         for(j=i; i*j<=50; j++)
 37                         {
 38                                 prime[i*j]=true;
 39                         }
 40                 }
 41         }
 42         int cnt=0;
 43         for(i=2; i<=50; i++)
 44         {
 45                 if(!prime[i])
 46                 {
 47                         ans[cnt++]=i;
 48                 }
 49         }
 50         while(scanf("%d %d",&p,&q)!=EOF)
 51         {     int s;
 52                 memset(coutt,0,sizeof(coutt));
 53                 for(s=2; s<=q; s++)
 54                 {
 55                         int cc=s;
 56                         int flag=0;
 57                         int t=0;
 58                         while(cc>1)
 59                         {
 60                                 if(cc%ans[t]==0&&flag==0)
 61                                 {
 62                                         flag=1;
 63                                         que.push(ans[t]);
 64                                         cc/=ans[t];
 65                                 }
 66                                 else if(cc%ans[t]==0)
 67                                 {
 68                                         cc/=ans[t];
 69                                 }
 70                                 else
 71                                 {
 72                                         t++;
 73                                         flag=0;
 74                                 }
 75                         }
 76                         int vv=0;
 77                         while(!que.empty())
 78                         {
 79                                 ask[vv++]=que.front();
 80                                 que.pop();
 81                         }
 82                         for(i=1; i<=(1<<vv)-1; i++)
 83                         {
 84                                 LL sum=1;
 85                                 int dd=0;
 86                                 for(j=0; j<vv; j++)
 87                                 {
 88                                         if(i&(1<<j))
 89                                         {
 90                                                 dd++;
 91                                                 sum*=ask[j];
 92                                         }
 93                                 }
 94                                 id[sum]=dd;
 95                                 coutt[sum]++;
 96
 97                         }
 98                 }
 99                 LL summ=0;
100                 for(i=2; i<=50; i++)
101                 {
102                         if(id[i]%2&&coutt[i]>=p)
103                         {
104                                 summ+=dp[coutt[i]][p];
105                         }
106                         else if(coutt[i]>=p)summ-=dp[coutt[i]][p];
107                 }if(summ>=10000)summ=10000;
108                 printf("%lld\n",summ);
109         }
110         return 0;
111 }
时间: 2024-10-19 16:26:49

1091. Tmutarakan Exams的相关文章

Ural 1091 Tmutarakan Exams

Tmutarakan Exams Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 109164-bit integer IO format: %lld      Java class name: (Any) University of New Tmutarakan trains the first-class specialists in mental arith

Ural 1091 Tmutarakan Exams【容斥原理】

题目链接: http://acm.timus.ru/problem.aspx?space=1&num=1091 题目大意: 给你两个整数K和S,从小于等于S的非负整数中选择K个数,并且K个数的最大公约数大于1, 问总共有多少组.(2 <= K <= S <= 50). 解题思路: 因为 2 <= K <= S <= 50,我们可以直接枚举质因数,求出从每个质因数的倍数中选择k个数 的组合数,累加起来即为方案个数,但是这样重复计算了很多情况. 例如:S = 20,

URAL 1091. Tmutarakan Exams(容斥原理)

题目链接 题意 : 给你两个数k,s,让你找k个数,这k个数都不大于s,并且这k个数的公约数大于1. 思路 : 枚举一下素数倍数,求组合数,最后容斥原理求最终结果. 当k=3,s=20的时候 : 2 : 2 4 6 8 10 12 14 16 18 20 3 :3 6 9 12 15 18 5 :5 10 15 20 只要从每个集合里边找出k个即可,这就是用组合数了.但是会有重复的,例如 2 : 6 12 18 3 : 6 12 18 这样就多加了一个,要再减去,所以就是容斥原理.加一个的减两个

URAL 1091. Tmutarakan Exams 容斥

从1到s选出k个数 他们的最大公约数大于1 求方案数 容斥 S(1)-S(2)+S(3) S(x)为选出k个数的公因子个数为x的数量 #include <cstdio> #include <cmath> #include <cstring> using namespace std; typedef long long LL; const int maxn = 55; int prime[maxn], vis[maxn]; int n, m; int get_primes

F - Tmutarakan Exams URAL - 1091 -莫比乌斯函数-容斥 or DP计数

F - Tmutarakan Exams 题意 : 从 < = S 的 数 中 选 出 K 个 不 同 的 数 并 且 gcd > 1 .求方案数. 思路 :记 录 一 下 每 个 数 的 倍 数 vector 存 储 ,最后从 2 开始 遍历 一遍每个数 ,从 他的倍数中 挑选 k个 组合数求解. 但是会有重复,因为 比如 K=2,S=15时 , 2倍数 : 2  ,4 , 6,  8, 10,  12, 14 ,   挑出了 这种情况 6 ,12,然后 从3的倍数 : 3, 6 ,9,12

2014 Super Training #3 H Tmutarakan Exams --容斥原理

原题: URAL 1091  http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有的数字都不能大于一个指定的数字S. 解法:可以考虑每个S内的素数,此素数和它的所有倍数构成一个集合,则可以在这些集合中任意去k个元素,C(n,k)即为这种情况下的方法种数,比如K = 3,S = 10, 则可以形成3个集合: {2,4,6,8,10} , {3,6,9}, {5,10} ,第一个集合C(5,

URAL1091---Tmutarakan Exams(dp)

University of New Tmutarakan trains the first-class specialists in mental arithmetic. To enter the University you should master arithmetic perfectly. One of the entrance exams at the Divisibility Department is the following. Examinees are asked to fi

Exams(二分求左界+贪心)

用力戳我直达原题:D - Exams 题意: 有N天和M门课程. 接下来给你N天的行为,0表示这一天只能预习,[1,m]表示这一天可以考这门课(当然这一天你也可以选择不考或者预习). 接下来给你M个数cost[i],代表第i门课需要预习cost[i]天才能PASS. 求从第一天起算,最少需要几天才能PASS所有功课,如果N天都PASS不了,则输出-1. 做法: 1.先判断用N天能否PASS,不能就输出-1. 2.low = m, high = n.求左界. 3-1 judge的时候,一门课肯定越

BZOJ 1091([SCOI2003]分割多边形-分割直线)

1091: [SCOI2003]分割多边形 Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 223  Solved: 82 [Submit][Status] Description 有一个凸p边形(p<=8).我们希望通过分割得到它.一開始的时候,你有一个n*m的矩形,即它的四角的坐标分别为(0,0), (0,m), (n,0), (n,m).每次你能够选择一条直线把当前图形分割成两部分,保留当中一个部分(还有一部分扔掉)分割线的长度为此直线在多边