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 ...
随机推荐
- Connections in Galaxy War----zoj3261
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 题意:有n个星球编号为0—n-1;能量分别为p[i]:有 ...
- iOS中textbox文本框清除圆角
在iOS.Mac safari中,所有的textbox, select, checkbox都会被强制美化为圆角.但在特殊情况下需要清除圆角时发现iOS中使用以下传统的css无效: border-rad ...
- Linux Packages Search
网站 : https://www.pkgs.org/ https://centos.pkgs.org/
- 010-spring cloud gateway-过滤器-自定义局部、全局过滤器、区别
一.自定义局部过滤器 自定义过滤器需要实现GatewayFilter和Ordered.其中GatewayFilter中的这个方法就是用来实现你的自定义的逻辑的 Mono<Void> fil ...
- HTML PX/EM换算工具 快捷键
换算工具:http://www.runoob.com/tags/ref-pxtoemconversion.html 快捷键:http://www.runoob.com/tags/html-keyboa ...
- 高性能mysql 4 ,5章
第4章 1:查询优化,多表连接时只取需要的列,要对select * 保持怀疑. 2:如果发现访问的数据行数很大,而生成的结果中数据行很少,那么可以尝试更复杂的修改 a: 使用覆盖索引,b: 更改架构, ...
- [py]监控内存并出图
监控内存出图 先将内存数据搞到数据库 已使用内存算法 used = int(total) - int(free) - int(butffers) - int(cache) pymysql模块使用 db ...
- git分支名一直带rebasing,如何去除
git分支名一直rebasing, 使用git rebase --continue git rebase --skip git reset --abort 都没有用, 最后直接删除 当前目录下的.gi ...
- [LeetCode] 561. Array Partition I_Easy tag: Sort
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- #C++初学记录(贪心算法#结构体#贪心算法)
贪心算法#结构体 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋 ...