比赛 |
20141105 |
评测结果 |
WWWWWWWWWW |
题目名称 |
韩信点兵 |
最终得分 |
0 |
用户昵称 |
Satoshi |
运行时间 |
0.003 s |
代码语言 |
C++ |
内存使用 |
0.32 MiB |
提交时间 |
2014-11-05 11:31:43 |
显示代码纯文本
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
ifstream in("HanXin.in");
ofstream out("HanXin.out");
long long n,m,sum=1,cost=1,s=0;
class node
{
public:
long long a;
long long b;
}rest[11];
long long mark(long long x,long long y)
{
//out<<x<<endl;
if(x<n)
{
if(x<0)
{
out<<-1<<endl;
return 0;
}
else
{
out<<n-x<<endl;
return 0;
}
}
mark(x-y,y);
}
long long compare(node x,node y)
{
if(x.a<=y.a)return true;
return false;
}
int main()
{
long long i,j;
in>>n>>m;
for(i=1;i<=m;i++)in>>rest[i].a>>rest[i].b;
sort(rest+1,rest+m+1,compare);
for(i=1;i<=m;i++)sum*=rest[i].a;
for(i=1;i<=m;i++)
{
cost=1;
for(j=1;j<=m;j++)
{
cost*=rest[j].a;
}
cost/=rest[i].a;
cost=rest[i].b*cost*(cost%rest[i].a);
s+=cost;
}
if(s>n)
{
if(s>=sum)
{
out<<n-(s-(s-n+sum-1)/sum*sum)<<endl;
}
else out<<-1<<endl;
}
if(s==n)out<<0<<endl;
if(s<n)out<<n-((n-s)/sum*sum)<<endl;
//mark(175,180);
in.close();
out.close();
return 0;
}