Advertisement
Saleh127

Light OJ 1298 / DP - Number Theory

Mar 5th, 2023
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. /***
  2.  created: 2023-03-05-16.51.54
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define all(we) we.begin(),we.end()
  13. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  14. #define nl '\n'
  15.  
  16. const ll mod=1e9+7;
  17. #define maX 100008
  18. bool marked[maX+2];
  19. vector<ll>p;
  20. void sieve()
  21. {
  22.     marked[0]=1;
  23.     marked[1]=1;
  24.     for(ll i=4; i<=maX; i+=2)
  25.     {
  26.         marked[i]=1;
  27.     }
  28.     p.push_back(2);
  29.     for(ll i=3; i<=maX; i+=2)
  30.     {
  31.         if(marked[i]==0)
  32.         {
  33.             p.push_back(i);
  34.             if(p.size()>500) break;
  35.             for(ll j=i*i; j<=maX; j+=i+i)
  36.             {
  37.                 marked[j]=1;
  38.             }
  39.         }
  40.     }
  41. }
  42.  
  43. ll dp[505][505];
  44.  
  45. ll solve(ll pr,ll st)
  46. {
  47.     if(pr<0) return 0;
  48.     if(st==0) return 1;
  49.     if(dp[pr][st]!=-1) return dp[pr][st];
  50.  
  51.     ll ans=(p[pr]*solve(pr,st-1)%mod + solve(pr-1,st))%mod;
  52.  
  53.     return dp[pr][st]=ans;
  54. }
  55.  
  56.  
  57. int main()
  58. {
  59.     ios_base::sync_with_stdio(0);
  60.     cin.tie(0);
  61.     cout.tie(0);
  62.  
  63.     sieve();
  64.  
  65.     memset(dp,-1,sizeof dp);
  66.     test
  67.     {
  68.         ll n,m,i,j,k,l;
  69.  
  70.         cin>>k>>n;
  71.  
  72.         ll ans=1;
  73.  
  74.         for(i=0;i<n;i++)
  75.         {
  76.             ans=ans*(p[i]-1);
  77.             ans%=mod;
  78.         }
  79.         ans*=solve(n-1,k-n);
  80.         cout<<"Case "<<cs<<": "<<ans%mod<<nl;
  81.     }
  82.     return 0;
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement