1978: [beijing2010] Number Game gametime limit: 10 sec memory limit: 64 MB
Submit: 650 solved: 400
[Submit] [Status] Description little C has just learned the division of phase and phase, and it is just a pleasure. This little P has come out again, leaving a difficult problem for little C. Give n numbers, using a1, a2... An. Now Small P allows small C to take the number in sequence, and the first number can be obtained at will. If AJ is obtained and the next number is AK (k> J), the AK must meet the requirement of gcd (AJ, ak) ≥ L. What is the actual number? Naturally, the more the better! Needless to say, this is not only a problem for small C, but also a problem for you. The first line of input contains two numbers N and L. In the next row, there are n numbers separated by spaces, which are A1, A2... An. Output contains only one row of data, which indicates the maximum number of data records that can be obtained based on the preceding method. Sample input5 6
7 16 9 24 6 sample output3hint
Select 3 numbers: 16, 24, and 6. Gcd (16,24) = 8, gcd (24,6) = 6.
2 ≤ L ≤ AI ≤ 1 000 000;
30% of Data n ≤ 1000;
100% of Data n ≤ 50 000
Source
Question:
This type of DP cannot be imagined... Is the general method of number theory still unknown...
Similar to the longest ascending sub-sequence, there is only a requirement that gcd must be greater than or equal to L, so that the root number N enumeration factor, and then DP
DP [I] indicates the maximum number of values that can be selected using I as the maximum public factor.
Update DP only when GCD> = L is met
Still do not understand? Why can I add the maximum value to each factor?
Alas? I suddenly understood it?
I indicates that if X and the last selected number GCD = I, a maximum of a few can be selected before, as long as the last selected number has an I factor, therefore, X can be updated to all X factors.
I finally figured it out. So happy!
Code:
1 #include<cstdio> 2 3 #include<cstdlib> 4 5 #include<cmath> 6 7 #include<cstring> 8 9 #include<algorithm>10 11 #include<iostream>12 13 #include<vector>14 15 #include<map>16 17 #include<set>18 19 #include<queue>20 21 #include<string>22 23 #define inf 100000000024 25 #define maxn 500+10026 27 #define maxm 1000000+10028 29 #define eps 1e-1030 31 #define ll long long32 33 #define pa pair<int,int>34 35 #define for0(i,n) for(int i=0;i<=(n);i++)36 37 #define for1(i,n) for(int i=1;i<=(n);i++)38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)42 43 #define mod 100000000744 45 using namespace std;46 47 inline int read()48 49 {50 51 int x=0,f=1;char ch=getchar();52 53 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}54 55 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}56 57 return x*f;58 59 }60 int n,m,ans,dp[maxm];61 62 int main()63 64 {65 66 freopen("input2.txt","r",stdin);67 68 freopen("output3.txt","w",stdout);69 70 n=read();m=read();71 for1(i,n)72 {73 int x=read(),y=0;74 for1(j,int(sqrt(x)))75 if(x%j==0)76 {77 y=max(y,dp[j]);78 y=max(y,dp[x/j]);79 }80 y++;81 for1(j,int(sqrt(x)))82 if(x%j==0)83 {84 if(j>=m)dp[j]=y;85 if(x/j>=m)dp[x/j]=y;86 } 87 }88 for2(i,m,maxm-1)ans=max(ans,dp[i]);89 printf("%d\n",ans);90 91 return 0;92 93 }View code
Bzoj1978: [beijing2010] Count game