23 条题解
-
14zone LV 10 @ 2016-07-27 08:54:23
n>m时
n!=1*2*.......*m*...*n;
n! % m =0
所以if(n>=m)n=m;
-
52017-06-03 15:59:06@
#include<iostream> using namespace std; int main(){ long long t=1,n,m,ans=0; cin >>n>>m; if (n>m) n=m; for (long long i=1;i<=n;++i) t=(t*i)%m,ans=(ans+t)%m; cout <<ans<<endl; return 0; }
-
02018-10-04 16:58:38@
p党来了
var
a,b,s,j:int64;
i:longint;
begin
read(a,b);
if a>b then a:=b-1;
s:=1;
for i:=1 to a do
begin
s:=s*i;
s:=s mod b;
j:=j+s;
end;
write(j mod b);
end. -
02018-08-20 09:24:44@
/* 做这题是在做比赛题目的时候, 看到这题数据范围,看到n这么大,直接哭了出来 然后就去看后面的题目了 果然我太弱了瞬间发现后面的也不会 然后一路暴力回来 突然脑子一动233333 发现如果计算的n,m n比m会更大,那么计算到m!,(m+1)!,(m+2)!的时候 一取模就没了啊 因为里面已经含有了m这个因子 所以余数一定是0 就可以不用考虑 然后就用的是对1到min(n,m)计算阶乘然后相加 发现诶不对这个复杂度明显超时啊 100万的范围估计就是O(n)? 突然发现自己傻逼了 我们计算1到min(n,m)的阶乘的时候 比如计算i的阶乘,那么i+1的阶乘就可以不用直接算了 就直接在保留的i的阶乘上乘上一个i+1就好了 以此类推 好像是一个很简单的技巧只能说做题目太少了 不过还好是想出来了 */ #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <iomanip> #include <cstdlib> using namespace std; long long n,m; int sum=1,ans; int main() { cin>>n>>m; for (long long i=1;i<=min(n,m);i++) { sum=(i*sum)%m; ans=(ans+sum)%m; } cout<<ans%m<<endl; return 0; }
-
02016-11-15 11:36:38@
数据有毒
编译成功测试数据 #0: WrongAnswer, time = 0 ms, mem = 560 KiB, score = 0
测试数据 #1: Accepted, time = 0 ms, mem = 556 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 556 KiB, score = 10
测试数据 #3: Accepted, time = 15 ms, mem = 556 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 552 KiB, score = 10
测试数据 #5: Accepted, time = 15 ms, mem = 556 KiB, score = 10
测试数据 #6: Accepted, time = 15 ms, mem = 556 KiB, score = 10
测试数据 #7: Accepted, time = 15 ms, mem = 556 KiB, score = 10
测试数据 #8: Accepted, time = 15 ms, mem = 556 KiB, score = 10
测试数据 #9: Accepted, time = 15 ms, mem = 556 KiB, score = 10
WrongAnswer, time = 90 ms, mem = 560 KiB, score = 90九次WA了。。。
-
02015-10-10 14:51:04@
###pascal code
program P1964;
var ans,i:longint;
sum,n,m:int64;
begin
read(n,m); sum:=1; if n>1000000 then n:=m;
for i:=1 to n do
begin
sum:=sum*i mod m; ans:=(ans+sum) mod m;
end;
write(ans);
end. -
02015-10-04 17:53:11@
#include <iostream> using namespace std; int main() { long long n,m; long long sum=0,temp=1; cin>>n>>m; for(int i=1;i<=min(n,m);i++) { temp*=i; temp%=m; sum+=temp; sum%=m; } cout<<sum<<endl; return 0; }
终于ac。。。
-
02015-10-04 10:27:46@
测试数据 #8?
-
02015-10-03 23:15:27@
Block code
#include<cstdio>
int main()
{
long long n,m,t=1,ans=0;
scanf("%lld%lld",&n,&m);
if(n>=m) n=m-1;
for(int i=1;i<=n;i++)
{
t=(t*i)%m;
ans+=t;}
ans%=m;
printf("%lld",ans);
return 0;}
用一个变量累成×,再一个变量累加就好了,累×变量实时取模,累加变量用long long,最后一次取模节省时间
当n>=m时,n! % m==0,所以后面的全部忽略掉,累加到(m-1)!过 -
02015-10-03 22:34:40@
#include <cstdio>
int main()
{
long long n;
int m;
scanf("%I64d %d", &n, &m);
if (n > m)
n = m;
int ans = 0;
int temp = 1;
for (int i = 1; i <= n; i++)
{
temp = temp * i % m;
ans = (ans + temp) % m;
}
printf("%d\n", ans);
return 0;
}为什么
测试数据 #0: Accepted, time = 0 ms, mem = 444 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 448 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 444 KiB, score = 10
测试数据 #3: Accepted, time = 15 ms, mem = 440 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 448 KiB, score = 10
测试数据 #5: Accepted, time = 0 ms, mem = 448 KiB, score = 10
测试数据 #6: Accepted, time = 0 ms, mem = 440 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 440 KiB, score = 10
测试数据 #8: WrongAnswer, time = 15 ms, mem = 440 KiB, score = 0
测试数据 #9: Accepted, time = 15 ms, mem = 448 KiB, score = 10
WrongAnswer, time = 45 ms, mem = 448 KiB, score = 90 -
02015-10-03 22:26:37@
program alisky;
var n,m,sum,tot,past,past1:qword;
i:longint;
begin
readln(n,m);
sum:=1;tot:=1;
for i:=2 to n do
begin
if (i>=m) then break;
tot:=tot*i mod m;
sum:=(sum+tot) mod m;
end;
writeln(sum);
end.
@少女夜夜
为什么这个第六组过不了 -
02015-10-03 22:08:45@
大神看看哪错了?爆了第九个点
#include<stdio.h>
#include<iostream>
using namespace std;
int x[1010000];
int main(){
int m;
long long n;
scanf("%lld%d",&n,&m);
if (n>m) n=m;
x[0]=1;
int sum=0;
for (int i=1;i<=n;i++)
{
x[i]=(x[i-1]*i)%m;
sum=(sum +x[i]) %m;
}
printf("%d\n",sum);
} -
-12016-12-08 20:04:19@
#include<cstdio> #include<cstring> using namespace std; main(){ int n,m; cin>>n>>m; int x=1; int d=0; for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ x=x*j; } d=x+d; x=1; } cout<<d; return 0; }
-
-12016-08-20 08:57:18@
{
N<=1000000000000000000,M<=1000000比M大的直接忽略。 (因为 mod M)
}
```PASCAL
var
i:longint;
N,M,tmp,ans:int64;begin
readln(N,M);
if N>M then N:=M-1;
tmp:=1;
for i:=1 to N do
begin
tmp:=(tmp*i)mod M;
if tmp=0 then break;
ans:=(ans+tmp) mod M;
end;
writeln(ans);
end.``` -
-12016-07-20 15:49:23@
一组大数据WA掉了,求大神帮忙看看!!!
#include<iostream>
using namespace std;
long long m,n;
int main ()
{
cin>>n>>m;
int ans=0,sum=1;
for (int i=1;i<=min(n,m);i++)
{
sum=(i*sum)%m;
ans=(ans+sum)%m;
}
cout<<ans;
return 0;
} -
-12016-07-20 15:34:04@
求指教,后面四组大数据过不了,怎么解决?
#include <iostream>
using namespace std;
int m,n;int main ()
{
cin>>n>>m;
int ans=0,sum=1;
for (int i=1;i<=n;i++)
{
sum=(i*sum)%m;
ans=(ans+sum)%m;
}
cout<<ans;
return 0;
} -
-12016-03-29 12:48:29@
希望大家帮忙解答一下,这段代码后四个全都WA
import java.util.Scanner;
java
class Main
{
public static void main(String[] args)
{
Scanner in= new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
long difficulty = 1;
long total=0;
int i = 0;
while (i!=n)
{
i++;
difficulty = difficulty*i%m;
total =(total+ difficulty)%m;
}
System.out.println(total);
}
}
-
-12015-11-05 19:55:59@
根据mod的性质,只要在阶乘和中找到一个和 mod m=0,那么之后的数据都可以舍去,因为 m! mod m=0 所以最大就找到m。
#include <iostream>
using namespace std;
long long int n,m,s=0,t=1;
int main()
{
cin>>n>>m;
if(n>=m)n=m;
for(long long int i=1;i<=n;i++)
{
t=(t*i)%m;
s=(s+t)%m;
}
cout<<s%m;
return 0;
} -
-12015-10-25 09:47:25@
/*
Author : Slience_K
Belong : C++
Pro : Vijos P 1964*/
#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std;
LL n , m , ans , sum;
int main(){
freopen( "P1964.in" , "r" , stdin );
scanf( "%I64d%I64d" , &n , &m );
n = min( n , m - 1 );
sum = 1;
ans = 0;
for( int i = 1 ; i <= n ; i++ ){
sum *= i;
sum %= m;
ans = ( ans + sum ) % m;
}
printf( "%I64d" , ans );
return 0;
} -
-12015-10-07 18:24:58@
#include<iostream>
int main()
{
unsigned long long n;
long m;
std::cin>>n>>m;
long long ans(1);
long long cnt(1);
for(long i=2;cnt!=0&&i<=n;i++)
{
cnt=(cnt*i)%m;
ans=(ans+cnt)%m;
}
std::cout<<ans;
return 0;
}