ACM-ICPC 2018 南京赛区网络预赛(A, J)

A  签到题

Alice, a student of grade 666, 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!:

k!=1×2×⋯×(k−1)×kk! = 1 \times 2 \times \cdots \times (k - 1) \times kk!=1×2××(k1)×k

We denote SSS:

S=1×1!+2×2!+⋯+S = 1 \times 1! + 2 \times 2! + \cdots +S=1×1!+2×2!++
(n−1)×(n−1)! (n - 1) \times (n-1)!(n1)×(n1)!

Then SSS module nnn is ____________

You are given an integer nnn.

You have to calculate SSS modulo nnn.

Input

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

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

It is guaranteed that 2≤n≤10182 \le n\le 10^{18}2n1018.

Output

For each test case, print an integer SSS modulo nnn.

Hint

The first test is: S=1×1!=1S = 1\times 1!= 1S=1×1!=1, and 111 modulo 222 is 111.

The second test is: S=1×1!+2×2!=5S = 1\times 1!+2 \times 2!= 5S=1×1!+2×2!=5 , and 555 modulo 333 is 222.

打个表就会发现结果等于 N-1;直接输出就是了;

F

题目链接 : https://nanti.jisuanke.com/t/30999

思路 : 欧筛 + 打表  ,水过的

#include<cstring>
#include<iostream>
#include<cstdio>
#include<ctime>
using namespace std;
#define N 20000005
#define ll long long
int vis[N];
int p[N], cnt, v[N];
ll sum[N];
void init(){
    int i, j, k;
    sum[1]= 1;
    for(i = 2; i < N; ++i){
        sum[i] = sum[i-1]+vis[i];  //  sum[i] 就是答案
        if (v[i]== 0){
            p[cnt++] = i;
        }
        for (j = 0; j < cnt && i * p[j] < N; j++){
            ll ans = p[j]*p[j];
            if(i%p[j]) {vis[i*p[j]] = vis[i]*2; v[i*p[j]] =1;}
            if(i%ans == 0) {vis[i*p[j]] =0; v[i*p[j]] =1;break;}    //  如果i含有至少两个相同的素数, 将 vis[i*p[j]] 置0;
            if(i%p[j]==0){vis[i*p[j]] = vis[i]/2; v[i*p[j]] =1;break;}
        }
    }
}


int main()
{
    vis[1] = 1;
    for(int i = 2; i < N; i++)
    {
        vis[i] = 2;
    }
    init();
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        cout << sum[n] << endl;
       /* for(int i = 1; i <= 300; i++)
        {
            cout << "vis " << i <<" : " << vis[i] << endl;
            cout << "sum "<< i << " : " << sum[i] << endl;

        }  */


    }
}

 大佬博客   : http://www.cnblogs.com/Dup4/p/9570883.html

猜你喜欢

转载自www.cnblogs.com/mrh-acmer/p/9571269.html