题意:把n个数(1-9)放到A集合和B集合里面去,使得A集合里面的数的数根为a,B集合里面的数的数根为b,也可以只放在A或B任一个集合里面。求方法总数。比如A={2,4,5},则A的数根为[2+4+5]=[11]=[2]=2

思路:一个数为a,则它的数根b=(a-1)%9+1=(digit-1)%9+1,digit是a的十进制各位上的数的和。如果存在解,那么任选一些数放到A集合里面,使得A集合的数根为a,那么B集合的数根一定为b。由公式可知,数根可以转化为余数来做,令dp[i][x]表示考虑前i个数,使得A里面数的和对9的余数为x的方法总数,则有dp[i][x]=dp[i-1][x]+dp[i-1][(x-a[i]+9)%9]。最后需要考虑只放一个集合的情况。

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; //#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=;i<a.size();i++)a[i]=b[i];} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-8; /* -------------------------------------------------------------------------------- */
template<int mod>
struct ModInt {
const static int MD = mod;
int x;
ModInt(ll x = ): x(x % MD) {}
int get() { return x; } ModInt operator + (const ModInt &that) const { int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); }
ModInt operator - (const ModInt &that) const { int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); }
ModInt operator * (const ModInt &that) const { return ModInt((long long)x * that.x % MD); }
ModInt operator / (const ModInt &that) const { return *this * that.inverse(); } ModInt operator += (const ModInt &that) { x += that.x; if (x >= MD) x -= MD; }
ModInt operator -= (const ModInt &that) { x -= that.x; if (x < ) x += MD; }
ModInt operator *= (const ModInt &that) { x = (long long)x * that.x % MD; }
ModInt operator /= (const ModInt &that) { *this = *this / that; } ModInt inverse() const {
int a = x, b = MD, u = , v = ;
while(b) {
int t = a / b;
a -= t * b; std::swap(a, b);
u -= t * v; std::swap(u, v);
}
if(u < ) u += MD;
return u;
} };
typedef ModInt<> mint; const int maxn = 1e5 + ; mint dp[maxn][]; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int T, n, a, b, x;
cin >> T;
while (T --) {
cin >> n >> a >> b;
dp[][] = ;
a = a % ;
b = b % ;
int tot = ;
for (int i = ; i <= n; i ++) {
scanf("%d", &x);
for (int j = ; j < ; j ++) {
dp[i][j] = dp[i - ][j] + dp[i - ][(j - x + ) % ];
}
tot = (tot * + x) % ;
}
mint ans = ;
if (tot == a && b > ) ans += ;
if (tot == b && a > ) ans += ;
if(tot == (a + b) % ) ans += dp[n][a].get();
printf("%d\n", ans.get());
}
return ;
}

[hdu5389 Zero Escape]数根的性质,DP的更多相关文章

  1. Digital root(数根)

    关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...

  2. Openjudge-NOI题库-数根

    题目描述 Description 数根可以通过把一个数的各个位上的数字加起来得到.如果得到的数是一位数,那么这个数就是数根.如果结果是两位数或者包括更多位的数字,那么再把这些数字加起来.如此进行下去, ...

  3. hdoj1013(数根,大数,九余数算法)

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

  4. 如何证明一个数的数根(digital root)就是它对9的余数?

    数根就是不断地求这个数的各位数之和,直到求到个位数为止.所以数根一定和该数模9同余,但是数根又是大于零小于10的,所以数根模9的余数就是它本身,也就是说该数模9之后余数就是数根. 证明: 假设有一个n ...

  5. BZOJ 2734 洛谷 3226 [HNOI2012]集合选数【状压DP】【思维题】

    [题解] 思维题,看了别人的博客才会写. 写出这样的矩阵: 1,3,9,... 2,6,18,... 4,12.36,... 8,24,72,... 我们要做的就是从矩阵中选出一些数字,但是不能选相邻 ...

  6. C++:函数求数根(总算写出来了。。。。)

    [问题描述] 数根问题递归求解:输入n个正整数(输入格式中第一行为整数个数n,后续行为n个整数),输出各个数的数根.数根的定义:对于一个正整数n,我们将它的各个位相加得到一个新的数字,如果这个数字是一 ...

  7. hdu5389 Zero Escape DP+滚动数组 多校联合第八场

    Zero Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  8. 【gdoi2018 day2】第二题 滑稽子图(subgraph)(性质DP+多项式)

    题目大意 [gdoi2018 day2]第二题 滑稽子图(subgraph) 给你一颗树\(T\),以及一个常数\(K\),对于\(T\)的点集\(V\)的子集\(S\). 定义\(f(S)\)为点集 ...

  9. BZOJ3530:[SDOI2014]数数(AC自动机,数位DP)

    Description 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3 ...

随机推荐

  1. Matlab学习-(2)

    1. 文件读取 在编写一个matlab项目时候,通常要导入很多不同格式的数据,下面我们来学习不同的导入函数.(1) 保存工作区MATLAB支持工作区的保存.用户可以将工作区或工作区中的变量以文件的形式 ...

  2. Navicat自动备份数据库

    @ 目录 Navicat自动备份数据库 备份与还原 修改备份位置 MySQL:5.7 Navicat:11 Windows10 重要数据库的定时备份是非常重要的,使用Navicat可以非常方便快捷地自 ...

  3. 多线程高并发编程(6) -- Semaphere、Exchanger源码分析

    一.Semaphere 1.概念 一个计数信号量.在概念上,信号量维持一组许可证.如果有必要,每个acquire()都会阻塞,直到许可证可用,然后才能使用它.每个release()添加许可证,潜在地释 ...

  4. [PHP][thinkphp5] 学习一:增删改查

    <?php namespace app\index\controller; use think\Controller; use think\Db; class Test extends Cont ...

  5. python 工具链 多版本管理工具 pyenv

    理解Shims pyenv会在系统的PATH最前面插入一个shims目录: $(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin 通过一个rehashing ...

  6. WTF Python:有趣且鲜为人知的Python特性

    Python 是一个设计优美的解释型高级语言,它提供了很多能让程序员感到舒适的功能特性.但有的时候,Python 的一些输出结果对于初学者来说似乎并不是那么一目了然. 这个有趣的项目意在收集 Pyth ...

  7. 微信小程序填坑---小程序支付

    因为公司刚刚重新做了网站,所以也吧公众号和小程序提上了日程,在公众号里面没有什么问题,直接按照官方文档进行代码编写.调试,然后就解决了公众号内支付的问题. 因为小程序提供了<webview> ...

  8. Install go1.5 for CentOS7

    https://golang.org/doc/install 下载好后,通过FTPS,传递到Linux里去,放哪里随便你自己,因为被墙了,所以在Windows通过旋风下载了这个玩意儿. 你也可以: w ...

  9. Jenkins(3)- 安装Jenkins过程中遇到问题的排查思路

    如果想从头学起Jenkins的话,可以看看这一系列的文章哦 https://www.cnblogs.com/poloyy/category/1645399.html 安装Jenkins过程中,可能会遇 ...

  10. Js实现将html页面或div生成图片

    参考:https://blog.csdn.net/huwei2003/article/details/79761580 今天要分享的是用html2canvas根据自己的需求生成截图,并且修复html2 ...