bzoj 1111 [POI2007] quaternary balance Wag digital Dp

1111: [POI2007] Quaternary Balance Wag

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 302  Solved: 201
[Submit][Status][Discuss]

Description

Mary is going to have a party and she is going to invite a lot of people to her party. And she was going to prepare some gold as a gift for each guest. In order not to hurt everyone's face, everyone must get the same gold. Mary will use a balance to weigh out the gold. She has a lot of weights, all of which have masses that are powers of 4. Mary places the gold on the left and the weight on the right disc or both discs. She wants to use the fewest possible weights for each weighing. And, he hoped, the same mass of gold would be weighed each time with a different weighing method. For a given mass n, Mary wants to know the minimum number of weights needed to complete the weighing, and wants to know how many ways the weighing can be done with that many weights.

Input

The input file contains only an integer representing the mass of gold that Mary wishes to give to each person. (1<=n<=10^1000)

Output

The output file contains only an integer representing the total possible weighing scales modulo 10^9.

Sample Input

166

Sample Output

3
Example explanation
There are three ways to weigh out 166. 166=64+64+16+16+4+1+1. 166=256-64-16-16+4+1+1. 166=256- 64-16-4-4-1-1.
 
 
Solution: %%claris
 

First convert n to quaternary, from low to high DP

f[i] means that this bit does not borrow from the next bit

g[i] means that this bit borrows from the next bit, but the borrowed one is not included in i

f[0]=0,g[0]=inf

f[i]=merge(f[i-1]+b[i],g[i-1]+b[i]+1)

g[i]=merge(f[i-1]+4-b[i],g[i-1]+3-b[i])

 

 1 #include<cstring>
 2 #include<cmath>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<iostream>
 6 
 7 #define ll long long
 8 #define mod 1000000000
 9 #define N 2000
10 
11 #define Wb putchar(' ')
12 #define We putchar('\n')
13 #define rg register int
14 using namespace std;
15 inline int read()
16 {
17     int x=0,f=1;char ch=getchar();
18     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
19     while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
20     return x*f;
21 }
22 inline void write(ll x)
23 {
24     if(x<0) putchar('-'),x=-x;
25     if (x==0) putchar(48);
26     int num=0;char c[20];
27     while(x) c[++num]=(x%10)+48,x/=10;
28     while(num) putchar(c[num--]);
29 }
30 
31 int n;
32 int a[N],b[N];
33 char s[N];
34 struct Node
35 {
36     int x,y;
37     Node(){}
38     Node(int _x,int _y){x=_x,y=_y;}
39     friend Node operator+(Node x,int a){return Node(x.x+a,x.y);}
40     friend Node operator+(Node x,Node y)
41     {
42         return x.x==y.x?Node(x.x,(x.y+y.y)%mod):(x.x<y.x?x:y);
43     }
44 }f[N],g[N];
45 
46 int main()
47 {
48     scanf("%s",s);int len=strlen(s);
49     for (int i=1;i<=len;i++)
50         a[i]=s[len-i]-'0';
51     while(len)
52     {
53         a[0]=0;
54         for (rg i=len;i>=1;i--)
55             a[i-1]+=(a[i]&3)*10,a[i]>>=2;
56         for (b[++n]=a[0]/10;len&&!a[len];len--);
57     }
58     f[0]=Node(0,1),g[0]=Node(N,0),n++;
59     for (rg i=1;i<=n;i++)
60         f[i]=(f[i-1]+b[i])+(g[i-1]+(b[i]+1)),
61         g[i]=(f[i-1]+(4-b[i]))+(g[i-1]+(3-b[i]));
62     write(f[n].y);
63 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325228452&siteId=291194637