Sichuan University freshman season E

Here Insert Picture Description

This problem is I did before the combined version is a thinking problem and Euler screen is really strong
there is a point that is the subject of a large number take to scare you
need to 1e9? ? ?
When the number is greater than p modulo result is 0, the number of the latter no need to consider p! !
I started with you still dare to believe congruence theorem using an array of characters? Then think about the title can not be doing things like
Euler sieve child
is particularly strong
in the middle also use the remainder theorem with the knowledge
to avoid overflow
on the code:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000;
bool check[maxn + 10];
int phi[maxn + 10];
int prime[maxn + 10];
int tot;   

void f3()
{
    memset(check, false, sizeof(check));
    check[1]=true;
    phi[1] = 1;
    tot = 0;
    for (int i = 2; i <= maxn; i++)
    {
        if (!check[i])
        {
            prime[tot++] = i;
            phi[i] = i - 1;
        }
        for (int j = 0; j < tot; j++)
        {
            if (i * prime[j] > maxn)
            {
                break;
            }
            check[i * prime[j]] = true;
            if (i % prime[j] == 0)
            {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
            else
            {
                phi[i * prime[j]] = phi[i] * (prime[j] - 1);
            }
        }
    }
    return ;
}

int main()
{
	f3();
	int t,n,p;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&p);
		int flag=min(n,p);
		long long ans=0,temp=1;
		for(int i=2;i<=flag;i++)
		{
			temp*=i;
			temp%=p;
			if(!check[i])
				ans=(ans+temp)%p;
		}
		printf("%d\n",ans);
	}
	return 0;
}

I take this board is a very good blogger that borrowed more than each other quite well getting hit a few times while I will not permit
wow whining I was too dishes
refueling

Published 54 original articles · won praise 4 · Views 891

Guess you like

Origin blog.csdn.net/weixin_45460987/article/details/103394312