http://acm.fzu.edu.cn/problem.php?pid=2179

Problem 2179 chriswho

Accept: 57    Submit: 136 Time Limit: 10000 mSec    Memory Limit : 327680 KB

fzuoj Problem 2179 chriswho-LMLPHP Problem Description

Chriswho很喜欢数字,特别喜欢一种数字,它能整除它的每一位数字(如果该位是0当做能整除),比如说126这个数字他就很喜欢,因为126%1=126%2=126%6=0。为了让更多的人喜欢这样的数,他决定出一道这样的题目。求解1到n(包括1和n)之间有多少个这样的数字。

fzuoj Problem 2179 chriswho-LMLPHP Input

第一行是一个整数t表示case数(不超过10组)。接下来t行,每行一个整数n(1<=n<=9*10^18)。

fzuoj Problem 2179 chriswho-LMLPHP Output

输出t行,每行包括一个数字,表示满足条件的数字个数。

fzuoj Problem 2179 chriswho-LMLPHP Sample Input

2 9 15

fzuoj Problem 2179 chriswho-LMLPHP Sample Output

9 13

fzuoj Problem 2179 chriswho-LMLPHP Source

FOJ有奖月赛-2014年11月

 
 
分析:

因为1-9的lcm是2520,所以对于每一个数模2520,余数如果是自己的lcm的倍数那么就是。

所以dp[i][j][k]代表位数为i,模2520为j,lcm为k的数个数。

枚举后面加的数字g,用dp[i][j][k]去更新dp[i+1][(j*10+g)%2520][lcm(g,k)]。

还要注意一点lcm的个数只有48个,所以就可以开一个数组映射。

写了2种写法,递推写法因为有大量状态浪费速度相当慢。

DFS则快很多

 
 
AC代码:
 
 
 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-11
#define NMAX 1000000005
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = ;
ret=;
while(((c=getchar())<''||c>'')&&c!='-');
if(c == '-')
{
flag = ;
c = getchar();
}
while(c>=''&&c<='') ret=ret*+(c-''),c=getchar();
if(flag) ret = -ret;
}
ll dp[][][];
int lcm[][],biao[],m[];
map<int,int>mp;
int gcd(int a,int b)
{
return (b == )?a:gcd(b,a%b);
} void init()
{
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
lcm[i][j] = i?i*j/gcd(i,j):j;
int nct = ;
for(int i = ; i <= ; i++) if(%i == )
{
m[i] = nct;
biao[nct++] = i;
}
biao[] = ;
// cout<<biao[48]<<endl;
} char ch[];
int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o4.txt","w",stdout);
#endif // GLQ
init();
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%s",ch);
int len = strlen(ch);
memset(dp,,sizeof(dp));
for(int i = ; i < ch[]-''; i++)
dp[][i][biao[i]] = ;
int ha[];
ha[] = ha[] = ch[]-'';
for(int i = ; i < len; i++)
{
int p = (i==len-)?ch[i]-''+:ch[i]-'';
for(int j = ; j < p; j++)
dp[i][(ha[]*+j)%][m[lcm[j][ha[]]]] = ;
ha[] = (ha[]*+p)%;
ha[] = lcm[p][ha[]];
int num = ch[i]-'';
for(int j = ; j < ; j++)
for(int k = ; k <= ; k++) if(dp[i-][j][k])
{
ll d = dp[i-][j][k];
for(int l = ; l <= ; l++)
{
int lc = m[lcm[l][biao[k]]];
// cout<<l<<" "<<lc<<" "<<lcm[l][biao[k]]<<endl;
dp[i][(j*+l)%][lc] += d;
}
}
}
ll ans = ;
for(int i = ; i < ; i++)
for(int j = ; j <= ; j++)
{
if(i%biao[j] == ) ans += dp[len-][i][j];
}
if(len == ) ans++;
ans--;
printf("%I64d\n",ans);
}
return ;
}
 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-11
#define NMAX 1000005
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = ;
ret=;
while(((c=getchar())<''||c>'')&&c!='-');
if(c == '-')
{
flag = ;
c = getchar();
}
while(c>=''&&c<='') ret=ret*+(c-''),c=getchar();
if(flag) ret = -ret;
}
ll dp[][][];
int mp[],ha[],lcm[][],shu[]; int gcd(int x, int y)
{
return y?gcd(y,x%y):x;
} void init()
{
memset(dp,-,sizeof(dp));
int nct = ;
for(int i = ; i <= ; i++) if(%i == )
{
ha[nct] = i;
mp[i] = nct++;
}
for(int i = ;i <= ; i++)
for(int j = ; j <= ; j++)
lcm[i][j] = (i==)?j:i*j/gcd(i,j);
} ll dfs(int yu, int lc, int len,int limit)
{
if(len == )
return yu%ha[lc] == ;
if(~dp[len][yu][lc] && !limit) return dp[len][yu][lc];
ll ans = ;
int p = limit?shu[len]:;
for(int i = p; i >= ; i--)
{
ans += dfs((yu*+i)%, mp[lcm[i][ha[lc]]],len-, limit && i == shu[len]);
}
if(!limit) dp[len][yu][lc] = ans;
return ans;
} int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o4.txt","w",stdout);
#endif // GLQ
int cas;
ll n;
init();
scanf("%d",&cas);
while(cas--)
{
scanf("%I64d",&n);
int nct=;
while(n)
{
shu[nct++] = n%10LL;
n /= 10LL;
}
// cout<<nct<<endl;
printf("%I64d\n",dfs(,,nct-,)-);
}
return ;
}
05-11 20:45