zoukankan      html  css  js  c++  java
  • 「ACM-ICPC 2018 南京站网络赛 A 题」An Olympian Math Problem

    描述

    传送门:我是传送门

    Alice, a student of grade 66, is thinking about an Olympian Math problem, but she feels so despair that she cries. And her classmate, Bob, has no idea about the problem. Thus he wants you to help him. The problem is:

    We denote k!k!: 

    k!=1×2××(k1)×k

    We denote SS: 

    S=1×1!+2×2!++(n1)×(n1)!

    Then SS module nn is __

    You are given an integer nn. 

    You have to calculate SS modulo nn.

    输入

    The first line contains an integer T(T1000)T(T≤1000), denoting the number of test cases. 

    For each test case, there is a line which has an integer nn.

    It is guaranteed that 2≤n≤10^18.

    输出

    For each test case, print an integer SS modulo nn.

    样例

    输入

    2
    2
    3

    输出

    1
    2

    Note

    The first test is: S=1×1!=1, and 1 modulo 2 is 1.

    The second test is: S=1×1!+2×2!=5 , and 5 modulo 3 is 2.

    证明

    首先我们知道

    1. n!=n(n1)! and  n!(modn)=0
    2. x0=xx−0=x

    下面开始证明(xjb推)
    1×1!+2×2!++(n1)×(n1)=1×1!+2×2!++(n1)×(n1)n!

    取出最后两项(n1)×(n1)!n!, 因为n!=n×(n1)!n!=n×(n−1)! ,因此可以将最后两项写为(n1n)×(n1)!=(1)×(n1)!

    现在原式变为1×1!+2×2!++(n2)×(n2)!(n1)!,再次取出最后两项(n2)×(n2)!(n1)!,此式可化为(n2)×(n2)!(n1)×(n2)!=(n2n+1)×(n2)!

    上式最终为(1)(n2)!(−1)∗(n−2)!
    原式变为1×1!+2×2!++(n3)×(n3)!(n2)!

    最终会变为(1)×1!=1

    (1)(modn)=n1

    代码

     1 #include <cstdio>
     2 using namespace std;
     3 int main() 
     4 {
     5     long long T,n;
     6     scanf("%lld",&T);
     7     while (T--) 
     8     {
     9         scanf("%lld", &n);
    10         printf("%lld
    ", n - 1);
    11     }
    12     return 0;
    13 }
  • 相关阅读:
    UVA
    UVA
    模板——扩展欧几里得算法(求ax+by=gcd的解)
    UVA
    模板——2.2 素数筛选和合数分解
    模板——素数筛选
    Educational Codeforces Round 46 (Rated for Div. 2)
    Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses
    Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence
    Educational Codeforces Round 46 (Rated for Div. 2) C. Covered Points Count
  • 原文地址:https://www.cnblogs.com/duny31030/p/14304979.html
Copyright © 2011-2022 走看看