PKU poj-1091

Source: Internet
Author: User

Fleas
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9591 Accepted: 2892

Description

Z the city is inhabited by many fleas. In the Z City Saturday Life Channel has an entertainment program. A flea will be asked to be in the middle of a high-altitude wire. The wire is long and can be seen as infinitely long. The host will send a card to the flea. The card is written with a n+1 natural number. The last one is M, and the first n is no more than M, and the same number is allowed on the card. Fleas can select a natural number s from the card at any time, and then jump to the left, or to the right, to the length of s units. His final task was to jump to a unit length from his left and pick up the gift that was located there.
For example, when the n=2,m=18, holding the card (10, 15, 18) of fleas, you can complete the task: He can jump to the left 10 unit length, and then jump to the left 3 times, each 15 unit length, and then to the right to jump 3 times, 18 units of length each time. and a flea holding a card (12, 15, 18) would not be able to jump to a unit length from his left.
After the determination of N and M, there were obviously altogether m^n cards. The question now is how many of these cards can be completed.

Input

Two integers n and m (n <=, M <= 100000000).

Output

The number of cards that can complete the task.

Sample Input

2 4

Sample Output

12

Hint

These 12 cards are:
(1, 1, 4), (1, 2, 4), (1, 3, 4), (1, 4, 4), (2, 1, 4), (2, 3, 4),
(3, 1, 4), (3, 2, 4), (3, 3, 4), (3, 4, 4), (4, 1, 4), (4, 3, 4)

Source

Hnoi 2001 Analysis: Very classical very classical number theory problem, in order to do this topic really and learned a Euclidean principle and the principle of repulsion. This problem is mainly studied. Reference: http://blog.csdn.net/crescent__moon/article/details/19327879?utm_source=tuicool&utm_medium=referral Problem Solving:

Give you two positive integers n,m, which allows you to find an equation for the length of n+1 satisfying conditions: a[1]*x1+a[2]*x2+a[3]*x3+...+a[n]*xn+a[n+1]*x (n+1) =1 (0<=a[i]<=m&&a [N+1]=m]

Ask you to find out how many situations meet this condition.

To make A[1]*x1+a[2]*x2+a[3]*x3+...+a[n]*xn+a[n+1]*m=1 (0<=a[i]<=m), then the greatest common divisor of a[1],a[2],a[3]....a[n+1] is 1.

To solve this problem, the knowledge you need to know has extended Euclid, dove nest Principle, and recursion for all permutation combinations.

Many blogs cite one example:

Example: n=2,m=360
360=3^2*2^3*5 all the non-satisfying conditions of the series, greatest common divisor is the product of 360 quality factors, as long as these combinations are removed, is the answer (not understand slowly)

Then we must first find all the quality factors of M, and then find out the total number of permutations, that is, the title of the M^n, and finally based on the pigeon Nest principle to obtain the final answer.

The formula is: ans=m^n-(n-tuple with odd number of male factors) + (n-tuple with even number of male factors). Take the example above.

ans=m^n-(n-tuple with male factor 2)-(n-Tuple with male factor 3)-(n-Tuple with male factor 5) + (n-tuple with male factor 2,3) + (n-tuple with male factor 2,5) + (n-tuple with male factor 3,5)-(n-tuple with male factor 2,3,5).

N-tuple with a common factor D, each position has (M/D) a choice (1 ~ m inside a multiple of m/d D), according to the multiplication principle, can be obtained the N-tuple (M/D) ^n with the common factor d.
1 //principle of tolerance and Euclidean principle2#include <stdio.h>3 4 #defineM 1000005 6 Long LongFactors[m],reorder[m];7 Long LongN,m,factornum,per;8 9 voidFactoring ()//decomposition of the mass factor, the presence of factors insideTen { Onefactornum=0; A     Long Longmax=m; -     inti =0; -      for(i=2; i*i<=max;i++) the     { -         if(max%i==0) factors[factornum++]=i; -          while(max%i==0) max/=i; -     } +     if(max!=1) factors[factornum++]=Max; - } +  A Long LongPowerLong Long Base,Long LongIndex//Beg X^y at { -     Long Longk=Base; -     Long Longi =0; -      for(i=1; i<index; i++) -         Base*=K; -     return Base; in } -  to voidDfsLong LongStartLong LongPosLong LongFactornum4reorder) + { -     Long Longi =0; the     if(pos==Factornum4reorder) *     { $         Long Longt=m;Panax Notoginseng          for(i=0; i<factornum4reorder; i++) -         { theT/=reorder[i];//T means that there are several numbers containing the mass factor on each bit +         } APer+=power (T,n);//How many of them are in total? the     } +     Else -     { $          for(I=start; i<factornum; i++)//recursive backtracking to solve all permutation combinations $         { -reorder[pos]=Factors[i]; -DFS (i+1, pos+1, Factornum4reorder); the         } -     }Wuyi } the  - intMain () Wu { -     Long Longi =0; About      while(SCANF ("%i64d%i64d", &n,&m)! =EOF) $     { - factoring (); -         Long Longans=Power (m,n); -          for(i=1; i<=factornum; i++) A         { +Per=0; theDfs0,0, i); -             if(i%2) Ans-=per;//if there is an n-tuple with an odd number of male factors, subtract $             ElseAns+=per;//if an n-tuple with an odd number of common factors is added the         } theprintf"%i64d\n", ans); the     } the     return 0; -}

PKU poj-1091

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.