Prime Number CodeForces - 359C (属于是数论)
Simon has a prime number x and an array of non-negative integers a1, a2, ..., an.
Simon loves fractions very much. Today he wrote out number on
a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: ,
where number t equals xa1 + a2 + ... + an.
Now Simon wants to reduce the resulting fraction.
Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).
The first line contains two positive integers n and x (1 ≤ n ≤ 105, 2 ≤ x ≤ 109)
— the size of the array and the prime number.
The second line contains n space-separated integers a1, a2, ..., an (0 ≤ a1 ≤ a2 ≤ ... ≤ an ≤ 109).
Print a single number — the answer to the problem modulo 1000000007 (109 + 7).
2 2
2 2
8
3 3
1 2 3
27
2 2
29 29
73741817
4 5
0 0 0 0
1
In the first sample . Thus, the answer to the problem is 8.
In the second sample, . The answer to the problem is 27,
as351 = 13·27, 729 = 27·27.
In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.
In the fourth sample . Thus, the answer to the problem is 1.
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const long long INF=1e9+7;
const long long maxn=101000; long long n,x;
long long a[maxn]; long long quick_mod(long long a,long long b)
{
long long ans=1;
a=a%INF;
while(b)
{
if(b&1)
ans=ans*a%INF;
a=a*a%INF;
b>>=1;
}
return ans;
} int main()
{
while(~scanf("%d %d",&n,&x))
{
long long sum1=0;
for(long long i=0; i<n; i++)
{
scanf("%d",&a[i]);
sum1+=a[i];
}
for(long long i=0; i<n; i++)
a[i]=sum1-a[i];
sort(a,a+n);
long long ans,j=1,cot=1,t;
for(j=1; j<=n; j++)
{
if(a[j]!=a[j-1])
{
if(cot%x)
{
ans=a[j-1];
break;
}
else
{
cot/=x;
a[j-1]+=1;
j--;
}
}
else cot++;
}
printf("%d\n",quick_mod(x,min(ans,sum1)));
}
return 0;
} #include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const long long INF=1e9+7;
const long long maxn=101000; long long n,x;
long long a[maxn]; long long gcd(long long a,long long b)
{
long long ans=1;
ans=ans%INF;
while(b)
{
if(b&1)
ans=ans*a%INF;
a=a*a%INF;
b>>=1;
}
return ans;
} int main()
{
while(~scanf("%I64d %I64d",&n,&x))
{
long long sum1=0;
for(long long i=0; i<n; i++)
{
scanf("%I64d",&a[i]);
sum1+=a[i];
}
for(long long i=0; i<n; i++)
a[i]=sum1-a[i];
sort(a,a+n);
long long ans,j=1,cot=1,t;
while(j<=n)
{
if(a[j]!=a[j-1])
{
if(cot%x)
{
ans=a[j-1];
break;
}
long long f=a[j-1]+1;
t=cot/x;
for(long long k=j-1,s=t; s>0; s--,k--)
a[k]=f;
j-=t;
j++;
cot=1;
}
else cot++,j++;
}
printf("%I64d\n",gcd(x,min(ans,sum1)));
}
return 0;
}
Prime Number CodeForces - 359C (属于是数论)的更多相关文章
- CodeForce 359C Prime Number
Prime Number CodeForces - 359C Simon has a prime number x and an array of non-negative integers a1, ...
- Relatively Prime Powers CodeForces - 1036F (莫比乌斯函数容斥)
Relatively Prime Powers CodeForces - 1036F Consider some positive integer xx. Its prime factorizatio ...
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 每日一九度之 题目1040:Prime Number
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- LintCode-Kth Prime Number.
Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eli ...
- 10 001st prime number
这真是一个耗CPU的运算,怪不得现在因式分解和素数查找现在都用于加密运算. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13 ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [Swift]LeetCode762. 二进制表示中质数个计算置位 | Prime Number of Set Bits in Binary Representation
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- 10_ for 练习 _ is Prime Number ?
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
随机推荐
- spring boot(一):入门
Spring Boot的优点 Spring Boot 是伴随着 Spring 4.0 诞生的,从字面理解,Boot是引导的意思,因此 Spring Boot 旨在帮助开发者快速搭建 Spring 框架 ...
- js_beautifier && css_beautifier for emeditor
// // Unpacker for Dean Edward's p.a.c.k.e.r, a part of javascript beautifier // written by Einar Li ...
- 游戏AI:行为树
Behavior Tree 行为树通过子Task的返回值决定整棵树的走向 Task 行为树上的每个节点都称为一个Task, 每个Task存在三种状态, success, failure, runnin ...
- 图片轮播器——jquery插件
下载:http://files.cnblogs.com/files/wordblog/jiaoben828.rar
- stegsolve使用探究
应该也不是工具的问题吧,更多的是图片.但是不知道咋取就写工具了. 比如:http://ctf5.shiyanbar.com/stega/chromatophoria/steg.png 我在想为毛要选择 ...
- C++学习之路(十):虚继承引入的执行效率
这篇文章不知道取啥名字了,暂且这样叫,直接看场景就明白了.节选自<深度探索C++对象模型> Point3d origin, *pt = &origin; (1)origin.x = ...
- Linux CentOS 6.9(图形界面)安装中文输入法
安装步骤 1. 切换到 root 用户,执行 yum -y install "@Chinese Support" 2. 退出终端,选择桌面菜单中 "System" ...
- linux系统分区参考
UPDATE: update is used to download package information from all configured sources. UPGRADE: upgrad ...
- MySQL之——如何添加新数据库到MySQL主从复制列表 【转】
转自 转载请注明出处:http://blog.csdn.net/l1028386804/article/details/54653691 MySQL主从复制一般情况下我们会设置需要同步的数据库,使用参 ...
- android 动态改变控件位置和大小 .
动态改变控件位置的方法: setPadding()的方法更改布局位置. 如我要把Imageview下移200px: ImageView.setPadding( ImageVie ...