nylg 640 Geometric Sum
Geometric Sum
- 描述
- Compute (a + a^2 + … + a^n) mod m.(a+a2+…an)mod
- 输入
- Three integers a,n,m.
(1≤a,n,m≤10^18)
It ends with EOF. - 输出
- The only integer denotes the result.
- 样例输入
-
2 2 1000000000
- 样例输出
-
6
- 来源
- Lepus
- 矩阵里也求过a+a^2+a^3+a^4.......
-
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
typedef long long LL; LL sum_mod(LL a,LL n,LL p)
{
LL ans=;
n=n%p;
while(n)
{
if(n&)
{
ans=ans+a;
if(ans>=p) ans=ans-p;
}
n=n>>;
a=(a+a)%p;
}
return ans;
}
LL solve(LL a,LL n,LL p)
{
LL p1=a,p2=a,ans,i;
vector<LL>Q;
while(n)
{
ans=(n&);
Q.push_back(ans);
n=n>>;
}
ans=Q.size()-;
for(i=ans;i>=;i--)
{
p1=sum_mod(p1,p2+,p);
p2=sum_mod(p2,p2,p);
if(Q[i]==)
{
p2=sum_mod(p2,a,p);
p1=(p1+p2)%p;
}
}
return p1;
}
int main()
{
LL a,n,p;
while(scanf("%lld%lld%lld",&a,&n,&p)>)
{
if(n==){
printf("%lld\n",a%p);
continue;
}
LL ans=solve(a,n,p);
printf("%lld\n",ans);
}
return ;
}
nylg 640 Geometric Sum的更多相关文章
- 基本概率分布Basic Concept of Probability Distributions 3: Geometric Distribution
PDF version PMF Suppose that independent trials, each having a probability $p$, $0 < p < 1$, o ...
- geometric median
The geometric median of a discrete set of sample points in a Euclidean space is the point minimizing ...
- Codeforces Round #Pi (Div. 2) C. Geometric Progression map
C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- 补写:Best Coder #85 1001 Sum(前缀和)
sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...
- sum (bestcoder)
sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...
- Codeforces Round #Pi (Div. 2) C. Geometric Progression
C. Geometric Progression time limit per test 1 second memory limit per test 256 megabytes input stan ...
- codeforces 789 B. Masha and geometric
链接 B. Masha and geometric depression 题意 给你一个等比数列的首项和公比q,然后给出一个上限l,m个数字,在这个等比数列里,小于l且没有在m个数字里面出现过的可以写 ...
- Java实现 LeetCode 640 求解方程(计算器的加减法计算)
640. 求解方程 求解一个给定的方程,将x以字符串"x=#value"的形式返回.该方程仅包含'+',' - '操作,变量 x 和其对应系数. 如果方程没有解,请返回" ...
- 图神经网络 PyTorch Geometric 入门教程
简介 Graph Neural Networks 简称 GNN,称为图神经网络,是深度学习中近年来一个比较受关注的领域.近年来 GNN 在学术界受到的关注越来越多,与之相关的论文数量呈上升趋势,GNN ...
随机推荐
- JAVA JDBC连接 SQLServer2012
连接数据库的java测试代码 import java.sql.*; public class ConManager { final static String cfn = "com.micr ...
- [转] 多线程 《深入浅出 Java Concurrency》目录
http://ifeve.com/java-concurrency-thread-directory/ synchronized使用的内置锁和ReentrantLock这种显式锁在java6以后性能没 ...
- Java基础(44):ArrayList使用详解
1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: a.动态的增加和减少元素 b.实现了IColle ...
- linux kernel.shmall shemax shemin 參數解釋
分类: oracle linux 2010-06-17 14:30 6193人阅读 评论(0) 收藏 举报 linuxoracleredhat数据库服务器x86 Linux X86-64操作系统,Or ...
- Listview和Gridview自定义分割线
1,ListView和GridView中的每一个条目都有分割线,属性android:footerDividersEnabled表示是否显示分割线,默认是为true,即可见.可以自定义的设置分割线的颜色 ...
- 查看在线EBS用户的相关信息
--查看在线EBS用户的相关信息 SELECT PAP.FULL_NAME, FU.USER_NAME, FAT.APPLICATION_NAME, FRT.RESPONSIBILITY_NAME, ...
- 夺命雷公狗---微信开发17----自定义菜单的事件推送,响应菜单的CLICK
废话不多说,index.php 代码如下所示: <?php /** * wechat php test */ //define your token require_once "com ...
- bootstrap, boosting, bagging 几种方法的联系
http://blog.csdn.net/jlei_apple/article/details/8168856 这两天在看关于boosting算法时,看到一篇不错的文章讲bootstrap, jack ...
- C++头文件为什么要加#ifndef #define #endif
#ifndef 在头文件中的作用 在一个大的软件工程里面,可能会有多个文件同时包含一个头文件,当这些文件编译链接成一个可执行文件时 ,就会出现大量“重定义”的错误.在头文件中实用#ifndef #de ...
- xUtils之ViewUtil
要使用xutils,首先要导入xutils类库. 其次要添加权限: <uses-permission android:name="android.permission.WRITE_EX ...