nylg 640 Geometric Sum

Geometric Sum

时间限制:1000 ms
 |  内存限制:65535 KB

难度:3


描述

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.......


 1 #include<iostream>
2 #include<stdio.h>
3 #include<cstring>
4 #include<cstdlib>
5 #include<vector>
6 using namespace std;
7 typedef long long LL;
8
9 LL sum_mod(LL a,LL n,LL p)
10 {
11 LL ans=0;
12 n=n%p;
13 while(n)
14 {
15 if(n&1)
16 {
17 ans=ans+a;
18 if(ans>=p) ans=ans-p;
19 }
20 n=n>>1;
21 a=(a+a)%p;
22 }
23 return ans;
24 }
25 LL solve(LL a,LL n,LL p)
26 {
27 LL p1=a,p2=a,ans,i;
28 vector<LL>Q;
29 while(n)
30 {
31 ans=(n&1);
32 Q.push_back(ans);
33 n=n>>1;
34 }
35 ans=Q.size()-2;
36 for(i=ans;i>=0;i--)
37 {
38 p1=sum_mod(p1,p2+1,p);
39 p2=sum_mod(p2,p2,p);
40 if(Q[i]==1)
41 {
42 p2=sum_mod(p2,a,p);
43 p1=(p1+p2)%p;
44 }
45 }
46 return p1;
47 }
48 int main()
49 {
50 LL a,n,p;
51 while(scanf("%lld%lld%lld",&a,&n,&p)>0)
52 {
53 if(n==1){
54 printf("%lld\n",a%p);
55 continue;
56 }
57 LL ans=solve(a,n,p);
58 printf("%lld\n",ans);
59 }
60 return 0;
61 }

nylg 640 Geometric Sum

时间: 2024-08-24 08:03:24

nylg 640 Geometric Sum的相关文章

NYOJ 640 Geometric Sum

Geometric Sum 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Compute (a + a^2 + - + a^n) mod m. 输入 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 别人的AC码: #include <stdio.h> typedef

sum

sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出NO 输入描述 输入文件的第一行有一个正整数T(1≤T≤101\leq T \leq 101≤T≤10),表示数据组数. 接下去有T组数据,每组数据的第一行有两个正整数n,m (1≤n

geometric median

The geometric median of a discrete set of sample points in a Euclidean space is the point minimizing the sum of distances to the sample points. This generalizes the median, which has the property of minimizing the sum of distances for one-dimensional

补写:Best Coder #85 1001 Sum(前缀和)

sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Description Given a sequence, you're asked whether there exists a consecutive subsequence whose sum is divisible by m. outpu

基本概率分布Basic Concept of Probability Distributions 3: Geometric Distribution

PDF version PMF Suppose that independent trials, each having a probability $p$, $0 < p < 1$, of being a success, are performed until a success occurs. If we let $X$ equal the number of failures required, then the geometric distribution mass function

sum (bestcoder)

sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出NO 输入描述 输入文件的第一行有一个正整数T(1≤T≤101\leq T \leq 101≤T≤10),表示数据组数. 接下去有T组数据,每组数据的第一行有两个正整数n,m (1≤n

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

129. Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

Leetcode 494 Target Sum 动态规划 背包+滚动数据

这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20))和一个数S c1a1c2a2c3a3......cnan = S, 其中ci(1<=i<=n)可以在加号和减号之中任选. 求有多少种{c1,c2,c3,...,cn}的排列能使上述等式成立. 例如: 输入:nums is [1, 1, 1, 1, 1], S is 3. 输出 : 5符合要求5种