ZOJ 3434 Hiiragi ' s sticks

Source: Internet
Author: User

Hiiragi ' s sticks Time limit: 3 Seconds Memory Limit: 65536 KB

hiiragi likes collecting sticks. It was said that she had collected  m  short sticks. One day, she is very excited that she found more sticks from a box under her bed. These new sticks have  n  different lengths, while for each length, there is infinite sticks of the same Length.

hiiragi Then came- Concatenates the new sticks in different ways to form long sticks of all possible lengths no longer than  L . Let ' s name the lengths a set  s . Her mother told she, "use each of your  m  short sticks to measure each of the lengths in  s< /em>. When you find a short sticks being able to measure a length in integer times, get the times it takes and denote it as  ; T . I'll give you a candy if you can find one on the uses the following rules to reduce  T   ; to 1. "

    1. For a number T, the find one of its prime factor p, calculate t1 = t/p.
    2. Use T1 as the new T and go back to the first step until T reaches 1.
Note:With the same short stick and the same length inS, ways is considered to be different iff the reduce processes is not all the same. But it could be counted many times if it's another short stick or the length differs.

So here, what is required to calculate how many candies Hiiragi can get in most.


The first line is T (≤10), the number of test cases.
Next is T test Cases. Each case contains 3 lines. The first line contains three integers n, m, l (n ≤20, m ≤105, l ≤106). The second line was n integers indicating the lengths of each kind of sticks found the bed. The third line is m integers indicating the sticks Hiiragi originally had. All sticks has length no shorter than 1 and no longer than L.


For each case, print one integer, the number of candies Hiiragi can get in most.

Sample Input
12 4 124 61 3 4 3
Sample Output

In the sample, using sticks of length 4 and 6 can make long sticks of S = {4, 6, 8, 10, 12}.
So, with short stick of length 1, Hiiragi can use the following ways
8-4, 2-1
6, 3, 1
6, 2, 1
4, 2, 1
To get 9 candies. Also, with short sticks of length 3, 4 and 3 she can get 2, 3 and 2 candies each. So all she can get is 9 + 2 + 3 + 2 = candies.

Test instructions: A little friend, already has the M root stick, one day he in the bed suddenly found n kinds of sticks, attention is kind, each kind of infinite, found n kinds of sticks can be arbitrarily pieced together into any length, range between 1 to L, get a set of S. Then using the previous m-stick to measure the long stick in S, the stick used to measure must be the factor of the stick in S, that is T=si/mi T to be an integer. After getting T, the child's mother said, if the child can divide t by the quality factor of T, so that T becomes 1, then can get a sugar, ask the most can get a few sugars, also up to how many methods of measurement.

#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <cmath > #include <vector> #include <algorithm>const int mm=1000009;typedef Long Long ll;using namespace Std;bool dp1[mm];//only 0 1 so with bool, save memory, int will explode.    ll Dp2[mm];vector<int>p[mm];int main () {int T;    int n,m,l;    int A;    for (int i=2;i<mm;i++) if (P[i].empty ()) {for (int j=i;j<mm;j+=i) p[j].push_back (i);//write down the quality factor of each number    } scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &n,&m,&l);        memset (dp1,0,sizeof DP1);        memset (dp2,0,sizeof DP2);            dp1[0]=1;//minus 0, indicating that the number is present, so the token is 1 for (int i=0;i<n;i++) {scanf ("%d", &a);            for (int j=a;j<=l;j++) dp1[j]|=dp1[j-a];//to find the elements in the S collection, labeled 1} for (int i=0;i<m;i++) { scanf ("%d", &a);//dp2[x] says that the number of methods that make up the X-length sticks dp2[a]++;//The current length, then t= (x/x) = 1, you can get a sugar} for (i NT I=2;i<=l;i++) {for (int j=0;j<p[i].size (); j + +) Dp2[i]+=dp2[i/p[i][j]; The recursive process, each time enumerating prime numbers is//12-(/2)->6-(/3)->2-(/2)-&GT;1,DP[12]+=DP[12/2];DP [6] has been calculated before//12-(/3)->4-        (/2)->2-(/2)-&GT;1,DP[12]+=DP[12/3];DP [4] have been found before} ll ans=0;        for (int i=1;i<=l;i++) {if (Dp1[i]) ans+=dp2[i];    } printf ("%lld\n", ans); } return 0;}

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

ZOJ 3434 Hiiragi ' s sticks

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.