zoukankan      html  css  js  c++  java
  • 51nod P1354 选数字 题解

    每日一题 day8 打卡

    Analysis

    背包+离散化

    这题是我们一次模拟赛的T2,结果我的暴力全TLE了。

    关键是如果将两个因数的乘积离散化在因数数组中之后等于这个乘积本身,说明a[j]*in离散化之后的下表可以通过+=ans[j]来计算

    然后就可以愉快地dp啦~~~

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define maxn 1000+10
     6 #define mod 1000000007
     7 using namespace std;
     8 inline int read() 
     9 {
    10     int x=0;
    11     bool f=1;
    12     char c=getchar();
    13     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
    14     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
    15     if(f) return x;
    16     return 0-x;
    17 }
    18 inline void write(int x)
    19 {
    20     if(x<0){putchar('-');x=-x;}
    21     if(x>9)write(x/10);
    22     putchar(x%10+'0');
    23 }
    24 int T;
    25 int n,k,cnt;
    26 int a[maxn],bf[maxn],ans[maxn];
    27 int main()
    28 {
    29     T=read();
    30     while(T--)
    31     {
    32         memset(ans,0,sizeof(ans));
    33         memset(a,0,sizeof(a));
    34         cnt=0;
    35         n=read();k=read();
    36         for(int i=1;i*i<=k;i++)
    37             if(k%i==0)
    38             {
    39                 if(i*i==k) 
    40                 {
    41                     a[++cnt]=i;
    42                     continue; 
    43                 }
    44                 a[++cnt]=i;
    45                 a[++cnt]=k/i;
    46             }
    47         sort(a+1,a+cnt+1);
    48         ans[1]=1;
    49         for(int i=1;i<=n;i++)
    50         {
    51             int in=read();
    52             if(k%in!=0) continue;
    53             for(int j=cnt;j>=1;j--)
    54             {
    55                 int ls=lower_bound(a+1,a+cnt+1,a[j]*in)-a;
    56                 if(a[ls]==a[j]*in) 
    57                 {
    58                     ans[ls]+=ans[j];
    59                     ans[ls]%=mod;
    60                 }
    61             }
    62         }
    63         printf("%d
    ",ans[cnt]);
    64     }
    65     return 0;
    66 } 

    请各位大佬斧正(反正我不认识斧正是什么意思)

  • 相关阅读:
    Go并发编程实战 第2版 PDF (中文版带书签)
    DirectShow 应用开发过程
    Filter 原理
    DirectShow 常用函数总结
    COM 编程基础
    DirectShow 简介
    C++ 静态库与动态库以及在 Windows上 的创建、使用
    DirectShow 学习方法
    Qt 编译配置相关总结
    环境变量对于 VS 有什么用?
  • 原文地址:https://www.cnblogs.com/handsome-zyc/p/11493798.html
Copyright © 2011-2022 走看看