记录编号 281450 评测结果 AAAAAAAAAA
题目名称 1786.韩信点兵 最终得分 100
用户昵称 AntiLeaf 是否通过 通过
代码语言 C++ 运行时间 0.000 s
提交时间 2016-07-11 19:08:45 内存使用 0.00 MiB
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define ULL unsigned LL
using namespace std;
namespace mine{
	inline int getint(){
		static int __c,__x;
		static bool __neg;
		__x=0;
		__neg=false;
		do __c=getchar();while(__c==' '||__c=='\n'||__c=='\r'||__c=='\t');
		if(__c=='-'){
			__neg=true;
			__c=getchar();
		}
		for(;__c>='0'&&__c<='9';__c=getchar())__x=__x*10+(__c^48);
		if(__neg)return -__x;
		return __x;
	}
	inline long long getll(){
		static long long __x;
		static char __c;
		static bool __neg;
		__x=0;
		__neg=false;
		do __c=getchar();while(__c==' '||__c=='\n'||__c=='\r'||__c=='\t');
		if(__c=='-'){
			__neg=true;
			__c=getchar();
		}
		for(;__c>='0'&&__c<='9';__c=getchar())__x=__x*10+(__c^48);
		if(__neg)return -__x;
		return __x;
	}
	inline unsigned long long getull(){
		static unsigned long long __x;
		static char __c;
		__x=0;
		do __c=getchar();while(__c==' '||__c=='\n'||__c=='\r'||__c=='\t');
		for(;__c>='0'&&__c<='9';__c=getchar())__x=__x*10+(__c^48);
		return __x;
	}
	inline void putint(int __x){
		static int __a[40],__i,__j;
		static bool __neg;
		__neg=__x<0;
		if(__neg)__x=-__x;
		__i=0;
		do{
			__a[__i++]=__x%10+48;
			__x/=10;
		}while(__x);
		if(__neg)putchar('-');
		for(__j=__i-1;__j^-1;__j--)putchar(__a[__j]);
	}
	inline void putll(long long __x){
		static int __a[40],__i,__j;
		static bool __neg;
		__neg=__x<0;
		if(__neg)__x=-__x;
		__i=0;
		do{
			__a[__i++]=__x%10+48;
			__x/=10;
		}while(__x);
		if(__neg)putchar('-');
		for(__j=__i-1;__j^-1;__j--)putchar(__a[__j]);
	}
	inline void putull(unsigned long long __x){
		static int __a[40],__i,__j;
		__i=0;
		do{
			__a[__i++]=__x%10+48;
			__x/=10;
		}while(__x);
		for(__j=__i-1;__j^-1;__j--)putchar(__a[__j]);
	}
}
using namespace mine;
const int maxn=15;
template<class T>T phi(T);
template<class T>inline T inv(T,T);
template<class T>inline T qpow(T,T,T);
int n;
ULL N,m[maxn],a[maxn],M=1,mi[maxn],ti[maxn],ans;
inline int MAIN(){
#define MINE
#ifdef MINE
	freopen("HanXin.in","r",stdin);
	freopen("HanXin.out","w",stdout);
#endif
	N=getull();
	n=getint();
	for(int i=1;i<=n;i++){
		m[i]=getull();
		a[i]=getull();
		M*=m[i];
	}
	for(int i=1;i<=n;i++){
		mi[i]=M/m[i];
		ti[i]=inv(mi[i],m[i]);
		ans=(ans+a[i]%M*ti[i]%M*mi[i]%M)%M;
	}
	if(ans>N){
		putchar('-');
		putchar('1');
	}
	else{
		while(ans+M<N)ans+=M;
		putull(N-ans);
	}
	return 0;
}
template<class T>inline T phi(T x){//O(n^1.5)
	T ans=x;
	for(int i=2;x^1;i++)if(x%i==0){
		ans=ans/i*(i-1);
		do x/=i;while(x%i==0);
	}
	return ans;
}
template<class T>inline T inv(T a,T p){
	return qpow(a,phi(p)-1,p)%p;
}
template<class T>inline T qpow(T a,T b,T p){
	LL ans=1;
	for(;b;b>>=1,a=a*a%p)if(b&1)ans=ans*a%p;
	return ans;
}
int hzoier=MAIN();
int main(){;}