HDU 5407 CRB and Candies (2015-year School competition 10th)

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Problem-Solving ideas: LCM (c (n,0), C (n,1),..., C (n,n)), the official solution is to convert LCM (,... n+1)/(n+1), but this practice is still too unfamiliar, may try to use the only decomposition theorem learned.


First of all, the n+1 number of LCM, in fact, is to find all the primes less than N, for each prime, which is the largest exponent, and then multiply them to get the LCM value. As a result, the problem translates into determining the maximum exponent for each pi. The Kummer theorem is used here to solve, and the Kummer theorem tells us the fact that P is exactly divisible by the exponent of C (n,k) equal to N and K are written in the P-ary number, the number of Ni<ki. The NI here refers to the term "I" when n is written in P-ary number. is actually equivalent to the number of "carry" occurrences that occur when the two numbers of n-k and K are added in accordance with the P-number. Since we are looking for the largest index, we want to carry it as much as possible. A few example calculations on the draft paper can be found, if the first occurrence of a rounding, then carry after the number of the remainder must be less than p-1, because in the P-system, the two addend maximum can only be p-1, however, if you want to occur after rounding the remaining number is equal to p-1, that is, the result of the addition should be p+ ( P-1), which is obviously impossible. Thus, as soon as the number is found, a K can be constructed so that all subsequent bits take place in the rounding. As a result, we have found the largest index.


Once the exponent is found, the product can be obtained by using the fast power calculation.

3. Code:

#define _crt_secure_no_warnings#include<iostream> #include <algorithm> #include <cassert> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include <queue> #include <deque> #include <cstdlib> #include <cstdio> #include < cstring> #include <cmath> #include <ctime> #include <cctype> #include <functional>using namespace Std; #define ME (s) memset (s,0,sizeof (s)) #define PB push_backtypedef Long long ll;typedef unsigned int uint;typed  EF unsigned long long ull;typedef pair <ll, int> p;const int MOD = 1e9 + 7;const int N = 1000000 + 10;int vis[n];int Primes[n];int idx;void seive () {int m = sqrt (N); for (int i = 2; I <= m; i++) if (!vis[i]) for (int j = i*i; j<n; J + = i) vis[j] = 1;for (int i = 2; i<n; i++) if (!vis[i]) primes[idx++] = i;} ll Pow_mod (ll a, int k) {ll res = 1;while (k>0) {if (K & 1) res = Res*a%mod;a = a*a%mod;k >>= 1;} return res;} int get_pow (int n, int p) {int cnt = 0;int Tmp[30];while (n>0) {tmp[cnt++] = N%p;n/= p;} int res = 0, flag = 0;for (int i = 0; i<cnt-1; i++) if (tmp[i]<p-1 + flag)//Find first a number less than p-1 {res++;flag = 1;} return res;} int main () {seive (); int t;for (scanf ("%d", &t); t--;) {int n;scanf ("%d", &n), ll ans = 1;for (int i = 0; i<idx&&primes[i] <= n; i++) {ans = ans*pow_mod (pri Mes[i], Get_pow (n, primes[i])% MOD;} printf ("%i64d\n", ans);}}

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

HDU 5407 CRB and Candies (2015-year School competition 10th)

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.