51nod 1013【快速幂+逆元】】的更多相关文章

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013 这是一个等比数列,所以先用求和公式,然后和3^(n+1)有关,有n比较大,所以用快速幂来解决,又有/2的操作,所以可以用费马小定理取逆元. #include<map> #include<queue> #include<stack> #include<cmath> #include<cstdio> #include&…
Problem A Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 463    Accepted Submission(s): 162 Problem Description 度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长很长的字符串.现在麻烦来了,他忘记了原来的字符串都是什么,神奇的是他竟然记得原来那些字符串的…
等比式子: Sn=(a1-an*q)/(1-q) n很大,搞一发快速幂,除法不适用于取膜,逆元一下(利用费马小定理) 假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p).刚好在本道题目一样适用,mod=1e9+7就是质数,那么gcd也就是=1,OK,那么b*k=1 这个逆元就等于 a^(mod-2); #include <cstdio> #include <stack> #include <iostream> #include <stri…
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 dup…
瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2121    Accepted Submission(s): 949 Problem Description 有一个无限大的矩形,初始时你在左上角(即第一行第一列),每次你都可以选择一个右下方格子,并瞬移过去(如从下图中的红色格子能直接瞬移到蓝色格子),求到第n行第m列的格子有几…
题意:有N个座位,人可以选座位,但选的座位不能相邻,且旋转不同构的坐法有几种.如4个座位有3种做法.\( 1≤N≤1000000000 (10^9) \). 题解:首先考虑座位不相邻的选法问题,如果不考虑同构,可以发现其种数是一类斐波那契函数,只不过fib(1)是1 fib(2)是3. 由于n很大,所以使用矩阵快速幂来求fib. 再者考虑到旋转同构问题,枚举旋转i (2π/n) 度,其等价类即\( gcd(i, n) \)种,那么可以得\[S(n)=\frac{1}{n}\sum_{d|n}^{…
题目地址:https://www.nowcoder.com/acm/contest/136/F 树状数组.快速幂.逆元的模板运用: #include<iostream> #include<cstdio> using namespace std; #define LL long long #define lowbit(x) x&-x ; ; int n, m; LL sum[N]; void read(int &x) { ; x = ; char ch = getch…
题目地址:https://www.nowcoder.com/acm/contest/136/J 解法一: 推数学公式求前n项和: 当k=1时,即为等差数列,Sn = n+pn(n−1)/2 当k≠1时,an+p/(k−1) = k(an−1+p/(k-1)),等比数列,Sn = (kn+1+(p−1)kn−(np+1)k+(n−1)p+1) / ((k-1)*(k-1)) 因为是除法取模,故除快速幂外还需逆元: Knowledge Point: 除法取模逆元:https://www.cnblog…
A Boring Question 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5793 Description Input The first line of the input contains the only integer T, Then T lines follow,the i-th line contains two integers n,m. Output For each n and m,output the answer i…
J-Super Sum 题目大意就是给定N个三元组<a,b,c>求Σ(a1^k1*a2^k2*...*ai^ki*..an^kn)(bi<=ki<=ci) 唉.其实题目本身不难的,怪我不知道当时怎么想的...本来观察式子很容易能得出结论: 比如<5,2,3>,<2,1,4>,<3,2,2>这组: 5^2*2^1*3^2+5^2*2^2*3^2+5^2*2^3*3^2+5^2*2^4*^32=5^2*(2^1+2^2+2^3+2^4)*3^2; 5…