UVA - 11490 Just Another Problem (因数分解)

There is a wise saying “Nothingis unfair in love and war”. Probably that is why emperors of ancient days usedto use many funny and clever
tricks to fool the opponents. The most commontechnique was to scare the opponent away by out numbering them with imaginarysoldiers. One of the funnier tricks (though hard to believe) was to give theown soldiers (not the opponent soldiers!) mild laxative dose
so that there is along queue of soldiers in front of toilet. Seeing this queue from a distancethe opponent would miscalculate the total number of soldiers and flee away.This was a famous trick given by famous clown of the sub-continent named Gopal(The infamous
“Gopal Bhar” to be precise). Another most common trick was tohave square shaped holes while arranging the soldiers in rows and columns. Suchan arrangement is shown below with 96 soldiers.



Fig: A valid layout with 96 soldiers. After arranging them 8 soldiers are missing.

The strict property of sucharrangement for this problem is as follows:

(a)    The soldiershave to be arranged in rows and columns.

(b)   The border of thearrangement has to be rectangular.

(c)    The arrangementshould have soldiers missing only from the inner layers.

(d)   The soldiers should bemissing only from two equal square shaped regions. So the number of missingsoldiers should be twice of a strictly positive square number. In the figureabove the number of missing soldier is 2*22.

(e)    The thickness ofsoldiers should be equal every where (except the corners) in horizontal andvertical directions along the missing square. For example in the figure abovethe thickness of soldiers in horizontal and vertical directions along themissing
square is always three.

Now given the total number ofsoldiers S your job is to determine whether or not they can be arrangedaccording to the above mentioned rules.

Input

The input file contains 1000lines of inputs. Each line contains a positive integer S (S<1000000000000),where S is the total number of soldiers.

Input is terminated by linecontaining a single zero.

Output

For each line of input produceone or more line of output. Each line reports one possible number of possiblemissing soldiers which would enable the desired arrange with the S soldiers. Asthe number of possible missing soldiers can be quite large so instead
of theactual number, the modulo 100000007 value should be printed. Also if there ismore than one possible value for number of missing soldiers the modulo100000007 values should be reported in descending order of the original numberof soldier (Not the modulo
value). If no such number of missing soldiers isfound print the line “No Solution Possible” instead.

Print a blank line after theoutput for each line of input. Look at the output for sample input for details.

Sample Input                              Output for Sample Input


96

102

11100

0


Possible Missing Soldiers = 8

 

No Solution Possible

 

Possible Missing Soldiers = 553352

Possible Missing Soldiers = 308898

Possible Missing Soldiers = 45000

Possible Missing Soldiers = 3528

 


Problem Setter: ShahriarManzoor

Special Thanks: Syed MonowarHossain.

题意:你有S个士兵,并打算把他们排成一个r行c列的矩阵,但是可以有两个”洞“的矩形方队,方队的边界必须是都有人站,且每个洞的4个方向的”厚度“都是一样的,求可能缺失的人数

思路:设厚度为a,那么可以得到:(3a+2c)*(2a+c) = S + 2*c^2, 推出 6a^2+7ac = S,然后就是枚举a了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef long long ll;
using namespace std;
const ll mod = 100000007;

ll n;

int main() {
	while (scanf("%lld", &n) != EOF && n) {
		ll m = sqrt(n/6+0.5);
		int flag = 0;
		for (ll i = 1; i <= m; i++) {
			if ((n-6*i*i) % (7*i) == 0) {
				ll cur = (n-6*i*i) / (7*i);
				if (cur <= 0)
					continue;
				cur %= mod;
				flag = 1;
				printf("Possible Missing Soldiers = %lld\n", (2*(cur*cur%mod))%mod);
			}
		}
		if (!flag)
			printf("No Solution Possible\n");
		printf("\n");
	}
	return 0;
}
时间: 2024-10-11 18:07:35

UVA - 11490 Just Another Problem (因数分解)的相关文章

uva 11490 - Just Another Problem(数学)

题目链接:uva 11490 - Just Another Problem 题目大意:有n个士兵,要排列成一个方阵,要求方阵尽量大,于是在方正的中间会空出两个正方形的区域,空出来的局域要求厚度相同,即正方形的四条边向相应方向均再有x行或者列. 解题思路:根据题意可以知道x(6x+7r)=n,x为厚度,r为正方形的边长.接着枚举x,x是n的因子. #include <cstdio> #include <cstring> #include <cmath> typedef l

UVA 11490 - Just Another Problem(数论)

11490 - Just Another Problem 题目链接 题意:有S个士兵,排成一个矩阵,矩阵中可以有两个洞,要求两个洞上下左右厚度一样,问能缺少士兵的情况数. 思路:推推公式,设厚度为a, 正方形为i, 那么(3 a + 2 i) (2 a + i) = S + 2 i i; 化简一下得到6 i i + 7 a i = S 由于S很大,所以去枚举厚度,这样只要枚举到sqrt(S)就够了,复杂度可以接受 代码: #include <stdio.h> #include <stri

UVa 11490 Just Another Problem

方法:数学 整除 根据推导发现新的方阵长为2*x+3*d, 宽为 x+2*d, 面积满足方程 (2*x+3*d)*(x+2*d) = S + 2*x*x. (d为thickness) (比较合理的方法) 继而得到 x = (S-6*d*d)/(7*d) 枚举d即可. code: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

UVA 10837 - A Research Problem(欧拉函数)

UVA 10837 - A Research Problem 题目链接 题意:给定phi(n),求最小满足的最小的n 思路:phi(n)=pk11(p1?1)?pk22(p2?1)?pk33(p3?1)....(p为质数),因此对于给定phi(n),先把满足条件phi(n)%(p?1)=0的素数全找出来,在这些素数基础上进行暴力搜索,枚举哪些素数用与不用,求出最小值.这样做看似时间复杂度很高,但是实际上,由于每次多选一个素数之后对于值是呈指数上升的,所以实际组合出来的情况并不会太多,因此是可行的

uva 10401 Injured Queen Problem(DP)

uva 10401 Injured Queen Problem 题目大意:这是一个变形的N皇后问题,皇后不再是占据一行一列以及斜线,她占据的只是她周围的一圈以及她所在的一列.题目给出一个含有问号,数字和字母的字符串.第i个字符是问号代表皇后在第i列的任意一行,若第i个字符是数字或字母X(1-F)代表皇后在第i列的X行.求满足该字符串的摆放方式的方法一共有几种. 解题思路:从第一列开始往后递推.dp[i][j]表示的是结合j - 1列的摆放方式,将皇后放在(i, j)的位置会有几种情况. #inc

uva 10837 - A Research Problem(欧拉函数+暴力)

题目链接:uva 10837 - A Research Problem 题目大意:给定一个phin,要求一个最小的n,欧拉函数n等于phin 解题思路:欧拉函数性质有,p为素数的话有phip=p?1;如果p和q互质的话有phip?q=phip?phiq 然后根据这样的性质,n=pk11(p1?1)?pk22(p2?1)???pkii(pi?1),将所有的pi处理出来,暴力搜索维护最小值,虽然看上去复杂度非常高,但是因为对于垒乘来说,增长非常快,所以搜索范围大大被缩小了. #include <cs

UVA 1363 Joseph&#39;s Problem

https://vjudge.net/problem/UVA-1363 n 题意:求 Σ  k%i i=1 除法分块 如果 k/i==k/(i+1)=p 那么 k%(i+1)=k-(i+1)*p= k-i*p-p = k%i-p 所以 商相同时,余数为等差数列 我不知道为什么交到vjudge一直WA,网上搜的题解交上去也WA #include<cmath> #include<cstdio> using namespace std; int main() { int n,k,i,j,

uva 10026 Shoemaker&#39;s Problem(贪心+排序)

虽然是个水题,但是在一些细节上wa了几次,好像不支持'\b'退格符号,我用在了输出空格那,结果wa了...白白 wa了几次...题意是看的题解..今天只写了两道题,速度有点慢,得加快了,以后得先认真读懂题目,题目读懂了 就相当于做出来一半然后仔细动脑想想,有想法了再敲,不能盲目的做题.另外,热烈祝贺今天c++ primer看到 了100页 思路: 这道题是让给的数据是每件工作需要做的天数和每耽误一天所需要的费用,让求一个序列使得付费最小,如果有相同答 案把字典树最小的输出...输出的是序号,该件