Dynamic Planning questions and planning questions

Source: Internet
Author: User

Dynamic Planning questions and planning questions

It's not a day or a day

I think it is very important for me to record the dp I have made (chao)... the fine stuff like DP.

Come on. Only when the thinking is strong enough can the question be split.

Editing ......

 

Tree DP:

1. Robots collect Metals

Analysis: set f [I] [j] to traverse the child tree of j, and the minimum cost of returning I to j is as follows. In particular, f [0] [I] indicates the cost of returning a robot after all nodes. The reason is that each node must and can only be transferred to contribute to the answer.

The state transition equation is divided into two types.

Initialization: f [I] [x] = sigma (f [0] [son_ I]);

Maintenance: f [I] [k] = min (f [I] [k], f [I-j] [son_ I] + f [j] [son_ I] + j * Edge_val;

It's really amazing to think about it.

#include    <iostream>#include    <cstdio>#include    <cstdlib>#include    <algorithm>#include    <vector>#include    <cstring>#include    <queue>#define LL long long int#define ls (x << 1)#define rs (x << 1 | 1)using namespace std;const int N = 100010;struct Node{int to;LL val;int next;}E[N];int n,S,K,tot,head[N];LL f[21][N];int gi(){  int x=0,res=1;char ch=getchar();  while(ch>'9'||ch<'0'){if(ch=='-')res*=-1;ch=getchar();}  while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();  return x*res;}LL gL(){  LL x=0,res=1;char ch=getchar();  while(ch>'9'||ch<'0'){if(ch=='-')res*=-1;ch=getchar();}  while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();  return x*res;}inline void link(int u,int v,LL w){  E[++tot]=(Node){v,w,head[u]};  head[u]=tot;}inline void dfs(int x,int fa){  for(int e=head[x];e;e=E[e].next)    {      int now=E[e].to;      if(now==fa)continue;      dfs(now,x);      for(int k=K;k>=0;--k){  f[k][x]+=f[0][now]+2*E[e].val;  for(int i=1;i<=k;++i)    f[k][x]=min(f[k][x],f[k-i][x]+f[i][now]+i*E[e].val);}    }}int main(){  //freopen("input.txt","r",stdin);  //freopen("output.txt","w",stdout);  n=gi();S=gi();K=gi();  for(int i=1;i<n;++i)    {      int u=gi(),v=gi();LL w=gL();      link(u,v,w);link(v,u,w);    }  dfs(S,S);printf("%lld\n",f[K][S]);    /*fclose(stdin);    fclose(stdout);*/  return 0;}

 

Backpack Problems

1. Count multiple backpacks

The test room only had a 01 backpack, but it was obviously useless.

Then it seems that RG says it's FFT? Weak tips ......

However, Nick spoke about a very clever practice: segmented.

In the test room, I was wondering if I could lose the branches of N/2. Nick said that I could boldly cut the roots.

Direct violent anus exists before the root number. In this way, the complexity of the first half is no pressure (it will not be analyzed) when the data volume does not exceed 2000 ).

For records after the root number, g [I] [j] indicates that I is used, and the volume is the number of j solutions. Then it is obvious that I can reach the root number n at most.

The transfer equation is also very clever. It gives me a sense of technology tree.

1. Add an item with the root number;

2. The value of each item in the backpack is changed to + 1;

Isn't it clever? I was scared/* Oh roar */. Remember g [1] [0] (G10 );

When calculating the answer, use the multiplication principle + the addition principle. Let's take a look at the first half and the second half.

#include    <iostream>#include    <cstdio>#include    <cstdlib>#include    <algorithm>#include    <vector>#include    <cstring>#include    <queue>#include    <cmath>#define LL long long int#define ls (x << 1)#define rs (x << 1 | 1)using namespace std;const int N = 100010;const int Mod = 23333333;LL f[5][N],g[320][N],n,lim,sum[N],now;int gi(){  int x=0,res=1;char ch=getchar();  while(ch>'9'||ch<'0'){if(ch=='-')res*=-1;ch=getchar();}  while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();  return x*res;}void pui(int x){  if(x<10)putchar(x+'0');  else pui(x/10),putchar(x%10+'0');}LL gL(){  LL x=0,res=1;char ch=getchar();  while(ch>'9'||ch<'0'){if(ch=='-')res*=-1;ch=getchar();}  while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();  return x*res;}inline void work1(){  for(int i=1;i<=lim;++i)    {      now^=1;memset(sum,0,sizeof(sum));      for(int j=0;j<=n;++j){  f[now][j]=(sum[j%i]+f[now^1][j])%Mod;  if(j<i*i)sum[j%i]=(sum[j%i]+f[now^1][j])%Mod;  else sum[j%i]=((sum[j%i]-f[now^1][j-i*i]+f[now^1][j])%Mod+Mod)%Mod;}    }}inline void work2(){  for(int i=0;i<=lim;++i)    for(int j=0;j<=n;++j)      {if(i&&i+j<=n)g[i][i+j]=(g[i][i+j]+g[i][j])%Mod;if(j+lim+1<=n)g[i+1][j+lim+1]=(g[i+1][j+lim+1]+g[i][j])%Mod;      }  ++g[1][0];}inline void calcans(){  LL Ans=0;  for(int i=0;i<=n;++i)    for(int j=1;j<=lim;++j)      Ans=(Ans+((LL)f[now][i]*(LL)g[j][n-i])%Mod)%Mod;  printf("%lld\n",Ans);}int main(){  n=gi();lim=sqrt(n);f[0][0]=g[0][0]=1;  work1();work2();calcans();return 0;}

 

 

Mathematics

1. Robot m

There are two positive solutions for this question: DP and recursive (actually a DP ). Regarding the second method, I found that its State settings were very clever, and the product of the Euler's function was applied to a very high realm.

It is easy to find that the independent number is the Euler's function. Therefore, the answer is to require all factors of a number:

1. The sum of Euler's functions composed of an odd number of different odd quality factors.

2. The sum of Euler's functions composed of even numbers of different odd-quality factors.

3. Sum of Euler's functions of all other factors.

Then, by the nature of the Euler's function, we find that the first two do not need to be evaluated.

It is useless to use the Mobius function, because there is no 2. DP can be considered;

The simple DP is the Euler's function that f [I] [j] indicates the number to be pushed to the I-th prime number, which is composed of j different odd prime numbers. Then the answer is "Two accumulators. This is also a clever use of product.

The second is more simple and crude, but this is the beauty of the world. Set Ans1 and Ans2 to the parity state of f respectively. Read all the way to recursive. The recursive style is also a brief introduction, which must make people not reach shiniao and shake me.

#include    <iostream>#include    <cstdio>#include    <cstdlib>#include    <algorithm>#include    <vector>#include    <cstring>#include    <queue>#define LL long long int#define ls (x << 1)#define rs (x << 1 | 1)using namespace std;const LL Mod = 10000;LL F[1010][1010],Ans1,Ans2,m,k;int gi(){  int x=0,res=1;char ch=getchar();  while(ch>'9'||ch<'0'){if(ch=='-')res*=-1;ch=getchar();}  while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();  return x*res;}void pL(LL x){  if(x<0)putchar('-'),pL(-x);  if(x<10)putchar(x+'0');  else pL(x/10),putchar(x%10+'0');}void pc(){putchar('\n');}LL gL(){  LL x=0,res=1;char ch=getchar();  while(ch>'9'||ch<'0'){if(ch=='-')res*=-1;ch=getchar();}  while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();  return x*res;}LL Qpow(LL d,LL z){  LL ans=1;  for(;z;z>>=1,d=d*d%Mod)if(z&1)ans=ans*d%Mod;  return ans;}int main(){  k=gL();m=1;  for(LL i=1;i<=k;++i)    {      LL p=gL(),e=gL();      m=m*Qpow(p,e)%Mod;      if(p==2)continue;      LL ans1=(Ans1+(Ans2+1)*(p-1))%Mod;      LL ans2=(Ans2+Ans1*(p-1))%Mod;      Ans1=ans1;Ans2=ans2;    }  pL(Ans2);pc();pL(Ans1);pc();pL(((m-Ans1-Ans2-1)%Mod+Mod)%Mod);  return 0;}

 

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.