ACM-ICPC 2018 焦作赛区网络预赛G Give Candies(隔板定理 + 小费马定理 + 大数取模,组合数求和)题解
题意:给你n个东西,叫你把n分成任意段,这样的分法有几种(例如3:1 1 1,1 2,2 1,3 ;所以3共有4种),n最多有1e5位,答案取模p = 1e9+7
思路:就是往n个东西中间插任意个板子,所以最多能插n - 1个,所以答案为2^(n - 1) % p。但是n最大有1e5位数,所以要用小费马定理化简。
小费马定理:假如p是质数,且gcd(a,p)=1,那么a (p-1)≡1(mod p)
所以我们只要把n - 1分解为n - 1 = k(p - 1) + m,而2^ k(p - 1) % p ≡1,所以2^(n - 1) % p = 2^m % p,化简完成。
所以我们把n - 1对p-1取模,用了大数取模
代码:
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = 1e5 + ;
const int seed = ;
const ll MOD = 1e9 + ;
const ll MOD1 = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
char num[maxn];
/*ll getmod(){
ll ans = num[0] - '0';
int len = strlen(num);
for(int i = 1; i < len; i++)
ans = (ans * 10 + num[i] - '0') % MOD1;
return ans - 1;
}*/
ll getmod(){
ll ans = num[] - '';
int len = strlen(num);
for(int i = ; i < len - ; i++)
ans = (ans * + num[i] - '') % MOD1;
if(len > ) ans = ans * + num[len - ] - '';
return (ans - ) % MOD1;
}
ll pmul(ll a, ll b){
ll ans = ;
while(b){
if(b & ) ans = (ans * a) % MOD;
a = a * a % MOD;
b >>= ;
}
return ans;
}
int main(){
int T;
ll n, p;
scanf("%d", &T);
while(T--){
scanf("%s", num);
p = getmod();
printf("%lld\n", pmul(, p));
}
return ;
}
ACM-ICPC 2018 焦作赛区网络预赛G Give Candies(隔板定理 + 小费马定理 + 大数取模,组合数求和)题解的更多相关文章
- 【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies
G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To make the pro ...
- ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies (打表找规律+快速幂)
题目链接:https://nanti.jisuanke.com/t/31716 题目大意:有n个孩子和n个糖果,现在让n个孩子排成一列,一个一个发糖果,每个孩子随机挑选x个糖果给他,x>=1,直 ...
- ACM-ICPC 2018 焦作赛区网络预赛 G Give Candies(高精度求余)
https://nanti.jisuanke.com/t/31716 题意 n颗糖果n个人,按顺序给每个人任意数目(至少一个)糖果,问分配方案有多少. 分析 插板法或者暴力打表后发现答案就为2^(n- ...
- ACM-ICPC 2018 焦作赛区网络预赛 G Give Candies
There are NNN children in kindergarten. Miss Li bought them NNN candies. To make the process more in ...
- ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)
There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...
- ACM-ICPC 2018 焦作赛区网络预赛 G题 Give Candies
There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more inte ...
- ACM-ICPC 2018 焦作赛区网络预赛
这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心)
ACM-ICPC 2018 徐州赛区网络预赛 G. Trace (思维,贪心) Trace 问答问题反馈 只看题面 35.78% 1000ms 262144K There's a beach in t ...
- ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
随机推荐
- Python开发【笔记】:获取mp3文件获取信息
import eyed3 def main(): mp3 = '8200031001_13429822982_1_00163e086216990b11e82403f00d3d9a.mp3' xx = ...
- ViewPager的OnPageChangeListener三个回调解析
一个native crash最后是重写ViewPager解决的,过程中发现这三个回调还有点绕,于是理了一下 ViewPager有两个操作,一个是用手指滑动翻页,一个是直接setCurrentItem( ...
- npm的用户名添加不上的原因
npm添加不上的错误e401 1.用cnpm提交,会提交的tao.org这个域名了,用npm提交试试 2.如果npm提交不上,那就查看配置文件配置中 registry=http://registry. ...
- sass和css的calc运算
1.sass不识别不同单位之间的计算,而calc则没问题. width: #{1rem - 2px}; /*出错*/ width: calc(1rem - 2px); 通常情况定制css样式,我不需要 ...
- Java——文件操作字符流和字节流的区别
转:http://blog.csdn.net/joephoenix/articles/2283165.aspx java的IO流分两种流 字节流 InputStream OutputStream 字符 ...
- qt——for循环里创建widget
在for循环里创建 widget,比如test类 不能使用 test t; 而要使用 test t = new test(): for (i=0;i<=3;i++) { QPushButton* ...
- 万恶之源 - Python初识函数
什么是函数 我们目前为止,已经可以完成一些软件的基本功能了,那么我们来完成这样一个功能:约x pint("拿出手机") print("打开陌陌") print( ...
- latex 转word
1:下载pandoc软件,支持多种文件格式互转. http://www.pandoc.org/installing.html#windows 2:下载zip包,解压,并将含有pandoc.exe的目录 ...
- for练习相关
for嵌套: 大圈套小圈思想:有一种重复的情况而这种情况每一次对应另外情况多次. ------------------------------------------------------- 例如: ...
- pythonon ddt数据驱动二(json, yaml 驱动)
这一篇主要是关于文件的数据驱动. 一.通过json文件驱动 @ddt class MyTest(unittest.TestCase): @file_data('test_data_list.json' ...