GTW likes function
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 318 Accepted Submission(s): 177
f(x)=∑xk=0(−1)k22x−2kCk2x−k+1,f0(x)=f(x),fn(x)=f(fn−1(x))(n≥1)
Note that φ(n) means Euler’s totient function.( φ(n) is an arithmetic function that counts the positive integers less than or equal to n that are relatively prime to n.)
For each test case, GTW has two positive integers — n and x , and he wants to know the value of the function φ(fn(x)) .
Each line of the input file indicates a test case, containing two integers, n and x , whose meanings are given above. (1≤n,x≤1012)
1 1 2 1 3 2
2 2 2
出题人:
由打表找规律可得,∑k=0x(−1)k22x−2kC2x−k+1k=x+1,所以显然fn(x)=n+x+1,
因此直接求φ(n+x+1)。时间效率O(T√n)
严格证明:
设an=∑k=0n(−1)k22n−2kC2n−k+1k
an=22n+∑k=1n(−1)k22n−2k(C2n−kk+C2n−kk−1)
=∑k=0n(−1)k22n−2kC2n−kk+∑k=0n−1(−1)k+122(n−1)−2kC2(n−1)−k+1k
设bn=∑k=0n(−1)k22n−2kC2n−kk,则bn=an+an−1
bn=22n+∑k=1n−1(−1)k22n−2k(C2n−k−1k+C2n−k−1k−1)+(−1)n
=4∑k=0n−1(−1)k22(n−1)−2kC2(n−1)−k+1k+∑k=0n−1(−1)k+122(n−1)−2kC2(n−1)−kk
=4an−bn−1
得an−an−1=an−1−an−2。因为a0=1,a1=1,所以an=n+1
证明比较费时,打表找规律能很快的得出解,所以本题的关键在于打表找规律。
#include
#include
using namespace std;
long long euler(long long n) //返回euler(n)
{
long long res=n,a=n;
for(long long i=2; i*i<=a; i++)
{
if(a%i==0)
{
res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
int main()
{
long long n,x,i;
while(~scanf("%lld%lld",&n,&x))
{
printf("%lld\n",euler(x+n+1));
}
return 0;
}