Ural 1091 Tmutarakan Exams "the principle of tolerance and repulsion"

Source: Internet
Author: User
Tags greatest common divisor

Topic Links:

http://acm.timus.ru/problem.aspx?space=1&num=1091


Main topic:

give you two integers k and s, select the number of K in a nonnegative integer equal to S, and the number of K greatest common divisor is greater than 1,

Ask how many groups there are in total. (2 <= K <= S <= 50).


Problem Solving Ideas:

Since 2 <= k <= S <= 50, we can directly enumerate the factorization and find out the number of K selected from each factorization multiple

Number of combinations, adding up is the number of scenarios, but this repeated calculation of a lot of cases.

For example: S = 20,k = 2.

Multiples of 2:2, 4, 6, 8, 10, 12, 14, 16, 18, 20

Multiples of 3:3, 6, 9, 12, 15, 18

Multiples of 5:5, 10, 15, 20

Multiples of 7:7, 14

Multiples of 11:11

......

if they are simply cumulative, most of them are repeated several times. You should remove the extra parts. equivalent to a multiple of 2,

multiples of 3 、... All as a collection of each part of the combination of the case, and then to find the set of the assembly.

if the number of K in the 2-fold set is P (2), the results are as follows:

P (2) +p (3) +...+p (23°c)-P (2*3)-P (2*5) .... (2*11) + P (2*3*5) + ...

Since s maximum number does not exceed 50,k minimum of 2, then 2*29 = > 50, so the mass factor is enumerated to 23.

The aggregation of a set is made by using the principle of tolerance.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    Std;int prime[22] = {2,3,5,7,11,13,17,19,23,29};int C (int n,int m) {if (n < m)//No error return 0;    m = min (M, n-m);    int ans = 1;        for (int i = 1; I <= m; ++i) {ans *= (n-i+1);    Ans/= i; } return ans;    int K,s;int Solve () {int ans = 0,top = 0;            for (int i = 0; i < ++i) {if (S/prime[i] < K) {top = i;        Break        }} for (int i = 1; i < (1 << top); ++i) {int odd = 0,mult = 1;  for (int j = 0; J < top;                ++J) {if (1 << j) & i) {odd++;            Mult *= Prime[j];        }} if (Odd & 1) ans + = C (S/mult, K);    else ans-= C (S/mult, K);    } if (ans <= 10000) return ans; else return 10000;} int main () {while (~SCANF ("%d%d ", &k,&s)) {printf ("%d\n ", Solve ()); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Ural 1091 Tmutarakan Exams "the principle of tolerance and repulsion"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.