https://vjudge.net/problem/URAL-2102
The hacker Michael develops breakthrough password manager, which is called KEK (Keeper of Encrypted Keys). A distinctive feature of KEK is excellent security. To achieve this, Michael had to develop innovative encryption scheme. For example, in the well-known RSA scheme the sum of prime powers in the factorization is equal to 2, whereas in Michael’s scheme this sum is equal to 20!
However, the current version of the KEK runs very slow. Michael has found out that the problem is in the function of checking a modulus for correctness. This function should take the number n and answer, whether the sum of prime powers included in the factorization of n is equal to 20. Can you do this quickly?
Remember that the factorization of an integer is the representation of it in the form like p 1 α1 · p 2 α2 · … · p k αk, where p i are prime numbers, and α i > 0. It is known that such representation is unique. Then the sum of powers looks like α 1 + α 2 + … + α k.
Input
The only line contains an integer n (1 ≤ n ≤ 10 18).
Output
If the sum of prime powers, included in the factorization of n, is equal to 20, then output “Yes”, otherwise output “No”.
Example
input | output |
---|---|
2 | No |
1048576 | Yes |
10000000000 | Yes |
题意:分解一个数,把他分解为质数乘积使每个质数的个数加起来总和为20
#include
#include
#include
using namespace std;
typedef long long ll;
int main()
{
long long n;
int cnt=0;
scanf("%lld",&n);
if(n==1)printf("No");
else
{
int flag=1;
for(ll i=2;i*i<=n;i++)
{
if(cnt>20||(ll)pow(i,20-cnt)>n)
{
flag=0;
break;
}
while(n%i==0)
{
cnt++;
n/=i;
}
}
if(!flag)
{
printf("No");
return 0;
}
if(flag&&n!=1)cnt++;
if(cnt==20)printf("Yes");
else printf("No");
}
return 0;
}
https://cn.vjudge.net/problem/CodeChef-STFM
中文题面:https://vj.ti12z.cn/6710047a10fbc6b648c6ec52355e06de?v=1562604395
For positive integer x let define function F(x) = 1 * (1! + x) + 2 * (2! + x) + … + x * (x! + x).
“k!” means factorial: k! = 1 * 2 * … * k
Chef wants to calculate F(p1) + F(p2) + … + F(pn).
As answer could be large, help him, calculate value modulo m.
Input
First line contains two integers n and m.
Next line contains n space separated integers pi.
Output
Output a single line containing one integer — calculated value modulo m.
Constraints
1 ≤ n ≤ 105
1 ≤ pi ≤ 1018
1 ≤ m ≤ 107
Subtasks
Subtask #1: 1 ≤ pi ≤ 6 (10 points)
Subtask #2: 1 ≤ pi ≤ 7 * 103 (25 points)
Subtask #3: m - prime number (25 points)
Subtask #4: original constraints (40 points)
Example
Input:
5 7
1 2 3 4 5
Output:
6
Explanation
F(1) = 1 * (1! + 1) = 2
F(2) = 1 * (1! + 2) + 2 * (2! + 2) = 3 + 8 = 11
F(3) = 1 * (1! + 3) + 2 * (2! + 3) + 3 * (3! + 3) = 4 + 10 + 27 = 41
F(4) = 1 * (1! + 4) + 2 * (2! + 4) + 3 * (3! + 4) + 4 * (4! + 4) = 5 + 12 + 30 + 112 = 159
F(5) = 1 * (1! + 5) + 2 * (2! + 5) + 3 * (3! + 5) + 4 * (4! + 5) + 5 * (5! + 5) = 794
F(1) + F(2) + F(3) + F(4) + F(5) = 2 + 11 + 41 + 159 + 794 = 1007
1007 modulo 7 = 6
思路:
康托展开X=a[n]∗(n−1)!+a[n−1]∗(n−2)!+…+a[i]∗(i−1)!+…+a[1]∗0!X=a[n]∗(n−1)!+a[n−1]∗(n−2)!+…+a[i]∗(i−1)!+…+a[1]∗0!
那么对照一下题目里的公式:
F(x)=1∗1!+2∗2!+3∗3!+…+x∗x!+(1+2+3…+x)∗xF(x)=1∗1!+2∗2!+3∗3!+…+x∗x!+(1+2+3…+x)∗x
后面的和大家都会算。
前面就是序列
x+1,x,x−1,…,2,1x+1,x,x−1,…,2,1 的康托展开。
显然x+1个元素的排列为(x+1)!,因为是从0开始计算,所以上面这个排列的康托展开就是(x+1)!−1
所以
可以发现,阶乘部分当n>m的部分%m后是肯定=0的
↓代码错的 还需修改
#include
#include
#include
using namespace std;
typedef long long ll;
const int N=1e7+7;
int f[N];
int main()
{
ll n,m,ans=0,p;
cin>>n>>m;
f[0]=1;
for(int i=1;i<=m;i++)f[i]=(f[i-1]*1LL*i)%m;
for(int i=0;i<n;i++)
{
ll tmpAns=0;
scanf("%lld",&p);
if(p&1)tmpAns=(p*(((p%m)*((p+1)/2)%m)%m))%m;//判断p的奇偶,奇数则如此,否则如下
else
tmpAns=((p%m)*((((p>>1)%m)*((p+1)%m))%m))%m;
ll fac;
if(p+1>=m)fac=0;
else fac=f[p+1];
tmpAns=(tmpAns+fac-1)%m;
ans=(ans+tmpAns)%m;
}
cout<<ans<<endl;
return 0;
}