Advertisement
a_pramanik

LUCIFER spoj (digit dp)

Apr 1st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. ///:-)
  2. #include <bits/stdc++.h>
  3. #define pb push_back
  4. #define ll long long int
  5. #define inf 2000000000
  6. #define infLL 9000000000000000000
  7. #define infULL 18446744073709551615
  8. #define Aktaruzzaman using
  9. #define pi (2.0*acos(0.0))
  10. #define Pramanik namespace std;
  11. #define vsort(v)   sort(v.begin(),v.end())
  12. #define ull unsigned long long int
  13. #define mem(a, b) memset(a, b, sizeof a)
  14. #define cf 100009
  15. #define MOD 1000000007
  16. #define pii pair<int, int>
  17.  
  18. //int dx[]={0,1,0,-1};
  19. //int dy[]={1,0,-1,0};
  20. //int dx[]={1,1,0,-1,-1,-1,0,1};
  21. //int dy[]={0,1,1,1,0,-1,-1,-1};
  22.  
  23. template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
  24. template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/gcd(a,b))*b;}
  25. template <class T> inline T is_prime(T a){if(a<2|a==4)return false;if(a==2||a==3||a==5)return true; T g=(T)sqrt(a)+1; for(T i=2; i<=g; i++){if(a%i==0)return false;}return false;}
  26. template <class T> inline T bigmod(T n,T p,T m){if(p==0)return 1; if(p%2)return ((n%m)*bigmod(n,p-1,m))%m; else {T c=bigmod(n,p/2,m);return ((c%m)*(c%m))%m;}}
  27.  
  28. Aktaruzzaman Pramanik
  29. ll vis[11][85][85][3];
  30. ll prm[102];
  31. ll a[12];
  32. void sv()
  33. {
  34.     prm[0]=1;prm[1]=1;
  35.     for(ll i=2; i<=100; i++){
  36.         if(prm[i]==0){
  37.             for(ll j = i*2; j<=100; j+=i){
  38.                 prm[j]=1;
  39.             }
  40.         }
  41.     }
  42. }
  43.  
  44. ll dp(ll pos, ll choto, ll osum, ll esum)
  45. {
  46.     if(pos==0){
  47.         if(esum>osum && prm[esum-osum]==0)return 1ll;
  48.         return 0;
  49.     }
  50.     if(vis[pos][osum][esum][choto]!=-1)return vis[pos][osum][esum][choto];
  51.  
  52.     ll xx=0, hi, pp;
  53.  
  54.     if(choto==1)hi = a[pos];
  55.     else hi=9;
  56.  
  57.     for(ll i=0; i<=hi; i++){
  58.            if(pos%2==0){
  59.             if(choto==1 && i==hi)xx+=dp(pos-1, 1,  osum, esum+i);
  60.             else xx+=dp(pos-1, 0, osum, esum+i);
  61.            }
  62.            else{
  63.              if(choto==1 && i==hi)xx+=dp(pos-1, 1,  osum+i, esum);
  64.             else xx+=dp(pos-1, 0, osum+i, esum);
  65.            }
  66.  
  67.     }
  68.  
  69.     return vis[pos][osum][esum][choto]= xx;
  70.  
  71. }
  72.  
  73. int main()
  74.  
  75. {
  76.     ios_base:: sync_with_stdio(0);
  77.     cin.tie(NULL);
  78.     cout.tie(NULL);
  79.  
  80.     ll t, i, j, k, l, r, x, y, sz;
  81.  
  82.     cin>>t;
  83.     sv();
  84.  
  85.     while(t--){
  86.         cin>>l>>r;
  87.         k = l-1;
  88.         sz=0;
  89.         while(k>0){
  90.             a[++sz]=k%10ll;
  91.             k/=10ll;
  92.         }
  93.  
  94.         mem(vis, -1);
  95.         x = dp(sz, 1, 0, 0);
  96.  
  97.  
  98.         sz = 0;
  99.         k = r;
  100.         while(k>0){
  101.             a[++sz] = k%10ll;
  102.             k/=10ll;
  103.         }
  104. //        for(ll i = sz; i>=1; i--)cout<<a[i];
  105. //        cout<<endl;
  106.         mem(vis, -1);
  107.         y = dp(sz, 1, 0, 0);
  108.         cout<<y-x<<endl;
  109.     }
  110.  
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement