Open judge C16H:Magical Balls 快速幂+逆元
C16H:Magical Balls
- 总时间限制:
- 1000ms
- 内存限制:
- 262144kB
- 描述
-
Wenwen has a magical ball. When put on an infinite plane, it will keep duplicating itself forever.
Initially, Wenwen puts the ball on the location (x0, y0) of the plane. Then the ball starts to duplicate itself right away. For every unit of time, each existing ball on the plane will duplicate itself, and the new balls will be put on the adjacent locations. The duplication rule of these balls is, during the i-th unit of time, a ball, which locates at (x, y), will duplicate ui balls to (x, y+1), di balls to (x, y-1), li balls to (x-1, y) and ri balls to (x+1, y).
The duplication rule has a period of M. In another words, ui=ui-M, di=di-M, li=li-M, ri=ri-M, for i=M+1,M+2,...
Wenwen is very happy because she will get many balls. It is easy to calculate how many balls she will get after N units of time. However, she wants to know the sum of x-coordinates and y-coordinates of all balls after N units of time. This is a bit difficult for her. Could you help her? Since the sum might be very large, you should give the sum modulo 1,000,000,007 to her.
- 输入
- The first line contains an integer T (1 ≤ T ≤ 25), indicating the number of test cases.
For each test case:
The first line contains four integers N (1 ≤ N ≤ 10^18), M (1 ≤ M ≤ 20,000), x0 and y0 (-10^18 ≤ x0,y0 ≤ 10^18);
Then follows M lines, the i-th line contains four integers: ui, di, li and ri (0 ≤ ui,di,li,ri ≤ 10,000).
- 输出
- For each test case, output one integer on a single line, indicating the sum of x-coordinates and y-coordinates of all balls after N units of time, modulo 1,000,000,007.
- 样例输入
-
1
2 2 1 1
2 0 0 0
0 0 0 1 - 样例输出
-
19
- 提示
- In the Sample Input:
Initially, there is 1 ball on (1,1).
After 1 unit of time, there is 1 ball on (1,1) and 2 balls on (1,2);
After 2 units of time, there is 1 ball on (1,1), 2 balls on (1,2), 1 ball on (2,1) and 2 balls on (2,2).
- Therefore, after 2 units of time, the sum of x-coordinates and y-coordinates of all balls is
(1+1)*1+(1+2)*2+(2+1)*1+(2+2)*2=19. - 题意:
- 给你一个球 初始位置在x0,y0
- 和一个周期函数
- 这个周期是m天 每天向上复制Ui个球 向下复制Di个球 向左复制Li个球,向右复制Ri个球
- 问你n天后 所有球的横纵坐标相加总和是多少
- 题解:
- Si表示第i天答案的总和, sumi 表示第i天球的总和
- S0为初始位置的答案即x+y
- 设定ai = ui+ri+li+di+1 , bi = ui+ri-li-di;
- 很容易得出
-
Si = S0 * (∏ai)+ ∑j ((∏ai)* bj / a[j]) ;
分别用逆元快速幂求解
-
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
const int N = 2e5+, M = 1e2+, MOD = 1e9+, inf = 2e9;
typedef long long ll; ll update(ll x) {
return ((x % MOD)+MOD)%MOD;
} ll quick_pow(ll x,ll p) {
if(!p) return ;
ll ans = quick_pow(x,p>>);
ans = ans*ans%MOD;
if(p & ) ans = ans*x%MOD;
return ans;
} ll inv(ll x,ll mo)
{
return quick_pow(x,mo-);
} int T;
ll n;
ll m;
ll U[N],D[N],R[N],L[N],A[N],B[N],Y[N];
int main()
{
scanf("%d",&T);
while(T--) {
ll x,y;
scanf("%lld%lld%lld%lld",&n,&m,&x,&y);
ll S0 = (x+y )%MOD;
ll allA = ;
for(int i=;i<=m;i++) {
scanf("%lld%lld%lld%lld",&U[i],&D[i],&L[i],&R[i]);
A[i] = (U[i] + R[i] + L[i] + D[i] + ) % MOD;
B[i] = (U[i] + R[i] - L[i] - D[i]) % MOD;
allA = allA * A[i] % MOD;
} ll W = ;
for(int i=;i<=m;i++) W = (W + B[i] * inv(A[i],MOD) ) % MOD;
//cout<<W<<endl;
ll ans = S0 * update(quick_pow(allA, n / m)) % MOD + update(n/m) * W % MOD * quick_pow(allA, n/m)% MOD;
ans %= MOD;
ll sum = quick_pow(allA,n/m);
//cout<<ans<<" "<<sum<<endl;
for(int i=;i<=n%m;i++) {
ans = (ans * (A[i]) % MOD + sum * B[i] % MOD) % MOD;
sum = sum * (A[i]) % MOD;
}
printf("%lld\n",(ans+MOD )%MOD); }
return ;
}
Open judge C16H:Magical Balls 快速幂+逆元的更多相关文章
- HDU 5685 Problem A | 快速幂+逆元
Problem A Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- hdu5698瞬间移动(杨辉三角+快速幂+逆元)
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- HDU 5868 Different Circle Permutation Burnside引理+矩阵快速幂+逆元
题意:有N个座位,人可以选座位,但选的座位不能相邻,且旋转不同构的坐法有几种.如4个座位有3种做法.\( 1≤N≤1000000000 (10^9) \). 题解:首先考虑座位不相邻的选法问题,如果不 ...
- 51nod 1013【快速幂+逆元】
等比式子: Sn=(a1-an*q)/(1-q) n很大,搞一发快速幂,除法不适用于取膜,逆元一下(利用费马小定理) 假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p).刚好 ...
- 【牛客小白月赛6】F 发电 - 树状数组&快速幂&逆元
题目地址:https://www.nowcoder.com/acm/contest/136/F 树状数组.快速幂.逆元的模板运用: #include<iostream> #include& ...
- 【牛客小白月赛6】 J 洋灰三角 - 快速幂&逆元&数学
题目地址:https://www.nowcoder.com/acm/contest/136/J 解法一: 推数学公式求前n项和: 当k=1时,即为等差数列,Sn = n+pn(n−1)/2 当k≠1时 ...
- HDU 5793 A Boring Question (找规律 : 快速幂+逆元)
A Boring Question 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5793 Description Input The first l ...
- 2016 Asia Jakarta Regional Contest J - Super Sum UVALive - 7720 【快速幂+逆元】
J-Super Sum 题目大意就是给定N个三元组<a,b,c>求Σ(a1^k1*a2^k2*...*ai^ki*..an^kn)(bi<=ki<=ci) 唉.其实题目本身不难 ...
- POJ 1845:Sumdiv 快速幂+逆元
Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16466 Accepted: 4101 Descripti ...
随机推荐
- 自定义控件之带水印的Textbox
代码地址: http://download.csdn.net/detail/u010312811/9553195 Windows消息处理: http://www.cnblogs.com/imstriv ...
- MongoDB 3.0 新特性【转】
本文来自:http://www.open-open.com/lib/view/open1427078982824.html#_label3 更多信息见官网: http://docs.mongodb.o ...
- 1.SpringMVC的简介和环境搭建
SpringMVC的简介: SpringMVC 和 Struts一样是一个MVC框架,和Spring无缝连接,和struts2类似, Spring MVC属于SpringFrameWork的后续产品, ...
- Python字符编码
http://www.runoob.com/python/python-strings.html ASCII Unicode UTF-8 # -*- coding: utf-8 -*- 格式化 %运算 ...
- 1.kvm的基本搭建
一.kvm简介 KVM 是指基于 Linux 内核的虚拟机(Kernel-based Virtual Machine). 2006 年 10 月,由以色列的Qumranet 组织开发的一种新的&quo ...
- BestCoder21 1002.Formula 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5139 题目意思:给出一个数 n,求出 f(n). 可以发现有以下规律: f(1) = 1! f(2) ...
- Luncence .Net 使用
public partial class Form1 : Form { public Form1() { InitializeComponent(); } //标准分词 private void bu ...
- EF没有同步更新(转)
不知道这算不算一个bug,当你新建一个从数据库生成的edmx时,他能正确的生成所有的tt文件,但是当你从数据库更新表结构时,他不能正确的更新tt文件,以建立Model1.edmx为例,在解决方案中展开 ...
- 【leetcode】Same Tree(easy)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- NEFU 1112 粉刷栅栏算法
题目链接 中文题 简单搜索题 例数据 输入 6 1 1 1 1 9 9 输出 3 注意是每一个递归搜索都返回一个min 而不是只有总的返回min #include <cstdio> #in ...