Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?
You are given a system of equations:
You should count, how many there are pairs of integers (a, b) (0 ≤ a, b) which satisfy the system.
A single line contains two integers n, m (1 ≤ n, m ≤ 1000) — the parameters of the system. The numbers on the line are separated by a space.
On a single line print the answer to the problem.
9 3
1
14 28
1
4 20
0
In the first sample the suitable pair is integers (3, 0). In the second sample the suitable pair is integers (3, 5). In the third sample there is no suitable pair.
水题。
#include<iostream>
#include<stdio.h>
using namespace std;
int a[100],m,n;
void init()
{
int i;
for(i=0;i<100;i++)
a[i]=i*i;
}
int main()
{
int sum,i;
init();
while(~scanf("%d%d",&n,&m))
{
sum=0;
for(i=0;a[i]<=n;i++)
{
if(i+(n-a[i])*(n-a[i])==m)
{
sum++;
}
// printf("%d %d\n",i,(n-a[i]));
}
cout<<sum<<endl;
}
return 0;
}
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?
You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes.
Each digit is allowed to occur in the number the same number of times it occurs in the set.
A single line contains a single integer n (1 ≤ n ≤ 100000) — the number of digits in the set. The second line contains n digits, the digits are separated by a single space.
On a single line print the answer to the problem. If such number does not exist, then you should print -1.
1 0
0
11 3 4 5 4 5 3 5 3 4 4 0
5554443330
8 3 2 5 1 5 2 2 3
-1
In the first sample there is only one number you can make — 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[100005];
int dp[100005][3];
int main()
{
int i,j,n,t;
scanf("%d",&n);
t=0;
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
if (a[i]==0) t++;
}
if (t==0)
{
printf("-1\n");
return 0;
}
if (t==n)
{
printf("0\n");
return 0;
}
sort(a,a+n);
memset(dp,-1,sizeof(dp));
dp[t-1][0]=0;
for (i=t;i<n;i++)
{
for (j=0;j<3;j++)
{
if (dp[i-1][j]==-1) continue;
dp[i][(j+a[i])%3]=max(dp[i-1][j]+1,dp[i][(j+a[i])%3]);
dp[i][j]=max(dp[i][j],dp[i-1][j]);
}
}
if (dp[n-1][0]==-1 || dp[n-1][0]==0)
{
printf("0\n");
return 0;
}
int tag=0;
for (i=n-1;i>=t;i--)
{
if (dp[i][tag]==dp[i-1][(tag+3-a[i]%3)%3]+1 && dp[i][tag]!=0)
{
printf("%d",a[i]);
tag=(tag+3-a[i]%3)%3;
}
}
for (i=0;i<t;i++)
{
printf("0");
}
printf("\n");
return 0;
}
C题没看懂题目意思,后面的题目就直接不想看。orz!