题目链接网址:https://vjudge.net/contest/293897
只选取部分题目的代码
Wolf and Rabbit
如果两者最大公因数不为1,那么一定有安全的点
#include <iostream>
#include <algorithm>
using namespace std;
int gcd(int a, int b)
{
return !b ? a : gcd(b, a%b);
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
int n, m;
cin >> m >> n;
if(gcd(m, n) == 1)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
Cake
把一个圆形蛋糕先分成p份,然后在分成q份,最后的结果就是p+q然后减去切的时候重合的点gcd(p,q)
#include <iostream>
#include <algorithm>
using namespace std;
int gcd(int a, int b)
{
return !b ? a : gcd(b, a%b);
}
int main()
{
ios::sync_with_stdio(false);
int p, q;
while(cin >> p >> q)
{
cout << p+q - gcd(p, q) << endl;
}
return 0;
}
美素数
先筛选出素数,然后打表
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int MAXN = (int) 1e6 + 7;
int prime[MAXN];
void init()
{
prime[0] = prime[1] = 1;
int len = sqrt(MAXN) + 1;
for(int i=2; i<len; i++)
{
if( !prime[i])
{
for(int j=i+i; j<MAXN; j+=i)
prime[j] = 1;
}
}
}
int ans[MAXN];
bool check(int num)
{
int res = 0;
while(num)
{
res += num % 10;
num /= 10;
}
if(prime[res])
return false;
return true;
}
int main()
{
ios::sync_with_stdio(false);
init();
ans[0] = 0;
for(int i=1; i<MAXN; i++)
{
if(prime[i] == 0 && check(i))
ans[i] = ans[i-1] + 1;
else
ans[i] = ans[i-1];
}
int t;
cin >> t;
int t1 = 1;
while(t--)
{
int l, r;
cin >> l >> r;
cout << "Case #" << t1++ << ":" << " " << ans[r] - ans[l-1] << endl;
}
return 0;
}
不要62
就是判断一个数字中有没有4或者62,就是每次对10和100取余就可以了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = (int) 1e6 + 7;
int a[MAXN];
int luck(int x)
{
while(x)
{
if(x%10 == 4 || x % 100 == 62)
return 0;
x /= 10;
}
return 1;
}
void init()
{
memset(a, 0, sizeof(a));
for(int i=1; i<MAXN; i++)
{
a[i] = a[i-1] + luck(i);
}
}
int main()
{
ios::sync_with_stdio(0);
init();
int n, m;
while(cin >> n >> m, n || m)
{
cout << a[m] - a[n-1] << endl;
}
return 0;
}
快速幂模板
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
ll pow_mod(ll a, ll n, ll mod)
{
ll res = 1;
while(n)
{
if(n&1)
res = res * a % mod;
a = a * a % mod;
n /= 2;
}
return res;
}
int main()
{
ios::sync_with_stdio(0);
ll a, b;
while(cin >> a >> b, a || b)
{
cout << pow_mod(a, b, 1000) << endl;
}
return 0;
}