HDU3652--容斥+数位dp

题意:含13子串且整除13的个数

思路:

n-不含13-不整除13+既不也不13(容斥)

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr strcat
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 clock_t __START,__END;
 27 double __TOTALTIME;
 28 void _MS(){__START=clock();}
 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 30 //***********************
 31 #define rint register int
 32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 34 #define mem(a,b) memset(a,b,sizeof(a))
 35 #define pr printf
 36 #define sc scanf
 37 #define ls rt<<1
 38 #define rs rt<<1|1
 39 typedef pair<int,int> PII;
 40 typedef vector<int> VI;
 41 typedef unsigned long long ull;
 42 typedef long long ll;
 43 typedef double db;
 44 const db E=2.718281828;
 45 const db PI=acos(-1.0);
 46 const ll INF=(1LL<<60);
 47 const int inf=(1<<30);
 48 const db ESP=1e-9;
 49 const int mod=(int)1e9+7;
 50 const int N=(int)1e6+10;
 51 
 52 //开头为i,长度为j,%13为k;
 53 int dp[10][15][15];
 54 ll ten[12];
 55 
 56 void Init(int len)
 57 {
 58     ten[1]=1;
 59     for(int i=2;i<=12;++i)ten[i]=ten[i-1]*10;
 60 
 61     dp[0][0][0]=1;
 62 
 63     for(int i=1;i<=len;++i)
 64     {
 65         for(int now=0;now<=9;++now)
 66         {
 67             for(int pre=0;pre<=9;++pre)
 68             {
 69                 for(int premain=0;premain<=12;++premain)
 70                 {
 71                     if(now==1&&pre==3)continue;
 72                     dp[now][i][(ten[i]*now+premain)%13]+=dp[pre][i-1][premain];
 73                 }
 74             }
 75         }
 76     }
 77 }
 78 
 79 int clac(int x)
 80 {
 81     int tx=x-1;
 82     int num[15];
 83     int ans=0,cnt=0;//既不也不的
 84     int ans2=0;//不含13的
 85     int ans1=tx-tx/13;//%13!=0的
 86     while(x)
 87         num[++cnt]=x%10,x/=10;
 88     num[cnt+1]=0;
 89     ll temp=0;
 90     for(int i=cnt;i>=1;--i)
 91     {
 92         temp+=num[i+1]*ten[i+1];
 93         for(int j=0;j<num[i];++j)
 94         {
 95             for(int k=0;k<=12;++k)
 96             {
 97                 if((temp+k)%13!=0&&!(j==3&&num[i+1]==1))
 98                     ans+=dp[j][i][k];
 99                 if(j==3&&num[i+1]==1)continue;
100                     ans2+=dp[j][i][k];
101             }
102         }
103         if(num[i]==3&&num[i+1]==1)break;
104     }
105     ans2--;
106     return tx-ans1-ans2+ans;
107 }
108 
109 bool J(int x)
110 {
111     int num[15];
112     int cnt=0;
113     while(x)
114         num[++cnt]=x%10,x/=10;
115     for(int i=cnt;i>1;--i)
116         if(num[i]==1&&num[i-1]==3)return 1;
117     return 0;
118 }
119 
120 int main()
121 {
122     Init(10);
123     int n;
124     while(~sc("%d",&n))
125         pr("%d\n",clac(n+1));
126     return 0;
127 }
128 
129 /**************************************************************************************/

 

posted @ 2020-03-06 21:13  ZMWLxh  阅读(181)  评论(0编辑  收藏  举报