fzuoj Problem 2179 chriswho
http://acm.fzu.edu.cn/problem.php?pid=2179
Accept: 57 Submit: 136 Time Limit: 10000 mSec Memory Limit : 327680 KB
Problem Description
Input
Output
Sample Input
Sample Output
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则快很多
//#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 ;
}
fzuoj Problem 2179 chriswho的更多相关文章
- fzuoj Problem 2129 子序列个数
http://acm.fzu.edu.cn/problem.php?pid=2129 Problem 2129 子序列个数 Accept: 162 Submit: 491Time Limit: ...
- fzuoj Problem 2182 水题
http://acm.fzu.edu.cn/problem.php?pid=2182 Problem 2182 水题 Accept: 188 Submit: 277Time Limit: 100 ...
- fzuoj Problem 2177 ytaaa
http://acm.fzu.edu.cn/problem.php?pid=2177 Problem 2177 ytaaa Accept: 113 Submit: 265Time Limit: ...
- FZUOJ Problem 2178 礼品配送
Problem 2178 礼物分配 题目链接: Click Here~ Problem Description 在双胞胎兄弟Eric与R.W的生日会上,他们共收到了N个礼物,生日过后他们决定分配这N个 ...
- FZUOJ Problem 2200 cleaning DP
Problem 2200 cleaning Problem Description N个人围成一圈在讨论大扫除的事情,需要选出K个人.但是每个人与他距离为2的人存在矛盾,所以这K个人中任意两个人的距 ...
- 【BZOJ】【2179】FFT快速傅里叶
FFT 做的第二道用到FFT的……好吧其实还是模板题-_-b 百度上说好像分治也能做……不过像FFT这种敲模板的还是省事=.= /*********************************** ...
- CSU2179: 找众数
Description 由文件给出N个1到30000间无序数正整数,其中1≤N≤10000,同一个正整数可能会出现多次,出现次数最多的整数称为众数.求出它的众数及它出现的次数. Input 输入文件第 ...
- 【BZOJ】2179: FFT快速傅立叶(fft)
http://www.lydsy.com/JudgeOnline/problem.php?id=2179 fft裸题.... 为嘛我的那么慢....1000多ms.. #include <cst ...
- 【智能算法】用模拟退火(SA, Simulated Annealing)算法解决旅行商问题 (TSP, Traveling Salesman Problem)
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 文章声明 此文章部分资料和代码整合自网上,来源太多已经无法查明出处,如侵犯您的权利,请联系我删除. 01 什么是旅行商问题(TS ...
随机推荐
- php面向对象之__toString()
似曾相识,在php面向对象编程之魔术方法__set,曾经介绍了什么是魔术方法,这一章又介绍一个魔术方法__tostring(). __toString()是快速获取对象的字符串信息的便捷方式,似乎魔术 ...
- Android 一键直接查看Sqlite数据库数据
转自:http://www.cnblogs.com/trinea/archive/2012/11/16/2773656.html 本文主要介绍Android开发中如何一键直接查看sqlite数据库中的 ...
- pro9
1.本次课学习到的知识点 C语言的几个基本数据类型 各种基本数据类型的常量的表现形式 C语言的表达式个中表达式的求解规则 2.实验过程中遇到的问题及解决方法: 不太理解完数的概念以及如何判断完数,另外 ...
- 蓝牙BLE ATT剖析(一)
一.概述 The attribute protocol allows a device referred to as the server to expose a set of attributes ...
- 编写category时的便利宏(用于解决category方法从静态库中加载需要特别设置的问题)
代码摘录自YYKit:https://github.com/ibireme/YYKit /** Add this macro before each category implementation, ...
- Xcode 之自己编译静态库
今天介绍下,如何利用Xcode,新建一个静态库,以及如何编译成i386.armv7.armv7s 等平台架构. 开发环境:MAC OS X 10.9.4 + Xcode 5.0.2 背景知识:库分两种 ...
- Linux下对各种压缩文件处理
Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲ta ...
- 转:ASP.NET MVC扩展之HtmlHelper辅助方法
1.什么是HtmlHelper辅助方法?其实就是HtmlHelper类的扩展方法,如下所示: namespace System.Web.Mvc.Html { public static class F ...
- Linux centos关机与重启命令详解与实战
Linux centos重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户使用) 4.s ...
- CentOS7 + linux kernel 3.10.94 compile 简记
Linux kernel 一直以其开源著称,可以自己编译选择合适的模块,针对特定的系统可以有不同的编译选项 来源 此次编译的内核版本为3.10.94,从官网www.kernel.org下载而来,自己虚 ...