锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
瀵逛簬姹備綑鏁扮殑闂鏄垜涓寮濮嬪氨蹇界暐浜嗙殑鍦版柟錛屽悗鏉ョ湅浜咹PF鐨勫崥瀹紝鎵嶇煡閬撴湁榪欐牱鐨勬妧宸?
(A+B)mod C = (A mod C) + (B mod C)
(AB) mod C = (A mod C) × (B mod C)
C緙栬瘧鍣?, 0MS 0KB , 濡傛灉浣犳妸鍓嶄袱鍙ュ畾涔夊彉閲忕殑璇彞瀵硅皟錛屽皢浼氳楄垂 20MS 鐨勬椂闂?br>
int main()
{
int i=0,j,K,N,temp,m;
long int A;
scanf("%d",&K);
for (; i<K; i++) {
scanf("%d",&N);
m=1;
A=0;
for (j=0; j<N; j++) {
scanf("%d",&temp);
temp%=9;
m=(m*temp)%9;
A=(A+m)%9;
}
printf("%d\n",(A+8)%9+1);
}
return 0;
}
]]>
#include <stdio.h>
using namespace std;
int main()
{
__int64 x,left,right,mid;
scanf("%I64d",&x);
left=0;
right=x+1;
while ((left+1)<right) // 浜屽垎鏌ユ壘
{
mid=(left+right)/2;
if(mid*mid<=x)
left=mid;
else
right=mid;
}
printf("%I64d",left);
return 0;
}
]]>
Input
Input contains integer number N (1<=N<=106)
棣栧厛鎯沖埌鐨勬槸鏋氫婦錛屾垜紜疄涔熻繖涔堝仛浜嗭紝5~8鏃犳灉錛屽浜?錛屾垜鐨?5騫存柟姝?Pentium M 涓婅窇浜嗚凍瓚?鍒嗗崐閽熶箣涔咃紝鎴戜竴寮濮嬭繕浠ヤ負姝誨驚鐜簡錛屽悗鏉ュ嚭鏉ュ叓涓粨鏋滐紝涔熷幓緗戜笂鏍稿浜嗕竴涓嬶紝紜疄婊¤凍鐨勫彧鏈夎繖鍏釜:
111111111
119357639
380642361
388888889
611111111
619357639
880642361
888888889
鎴戞病鏈夊榪囨暟璁猴紝鎵懼埌涓綃囧崥鏂囷紝瀵逛簬鏁拌瑙f硶璁茬殑榪樼畻娓呮錛屼笉榪囩敤pascal鍐欏氨錛屾垜鑷繁鐢–鍐欎簡涓涓嬶紝絎笁嬈C浜嗐傚崥鏂囬摼鎺ュ涓嬶細http://blog.csdn.net/Skyprophet/archive/2009/10/05/4634801.aspx
There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Given first N elements of that sequence. You must determine amount of numbers in it that are divisible by 3. (鎵懼嚭榪欎釜鏁板垪1-N鍙峰厓绱犱腑鑳借3鏁撮櫎鐨勬湁澶氬皯涓?
Input
Input contains N (1<=N<=231 - 1).
涓涓暟涔樹互10浠ュ悗錛屾ā3鍚庣粨鏋滀笉鍙橈紝絎?錛?錛?錛?1錛?4錛?7........欏規弧瓚寵姹?br>
Nearly prime number is an integer positive number for which it is possible to find such primes P1 and P2 that given number is equal to P1*P2. There is given a sequence on N integer positive numbers, you are to write a program that prints “Yes” if given number is nearly prime and “No” otherwise.
Input
Input file consists of N+1 numbers. First is positive integer N (1£N£10). Next N numbers followed by N. Each number is not greater than 109. All numbers separated by whitespace(s).
Output
Write a line in output file for each number of given sequence. Write “Yes” in it if given number is nearly prime and “No” in other case.
涓嶇敤綆early prime numbers 鍒板簳鏄釜浠涔堟暟錛屾諱箣鏄袱涓川鏁扮殑涔樼Н灝卞浜嗐傛灇涓劇殑鑼冨洿: 2 ~ 32000 (104.5 )
鎴戠殑鎬濊礬鏄? 棣栧厛鎶?~32000涔嬮棿鐨勬墍鏈夌礌鏁伴兘瀛樻斁鍦ㄤ竴涓暟緇勯噷錛岀劧鍚庡綋浣犺緭鍏ヤ竴涓暟鎹椂錛屽厛璁╁叾閫愪竴妯¢櫎榪欎釜鏁扮粍閲岀殑绱犳暟錛屼竴鏃︽ā闄ょ粨鏋滀負0錛屽垯璁$畻鍑轟粬浠殑鍟嗭紝鍐嶅垽鏂晢鏄惁涔熶負绱犳暟銆?strong>娉ㄦ剰鏁版嵁鏄袱涓暟鐨勫鉤鏂圭殑澶勭悊
#include <stdio.h>
#include <math.h>
int prime[30000],M=0;
int isP(int n) //鍒ゆ柇鏄惁涓虹礌鏁?闈炲父綺劇‘鑰岄珮鏁?/span>

{
int i=2,t=sqrt(n);
if ((n != 2 && !(n % 2)) || (n != 3 && !(n % 3)) ||
(n != 5 && !(n % 5)) || (n != 7 && !(n % 7)))
return 0;
for (; i<=t; i++)
if (n%i == 0)
return 0;
return 1;
}
int isNP(int n)

{
int i=0,t=sqrt(n),r;
for (; prime[i]<=t; i++) // 騫蟲柟鍦ㄦ澶勭悊
{
if (n%prime[i] == 0) // 妯¢櫎璇曞晢
{
r=n/prime[i]; // 姹傚晢
if (isP(r))
return 1;
}
}
return 0;
}
int main()

{
int i=3,N,m;
for (prime[M++]=2; i<32000; i+=2)
if (isP(i))
prime[M++]=i;
scanf("%d",&N);
while (N--)
{
scanf("%d",&m);
if (m==6)
printf("Yes\n"); // 紼嬪簭涓敮涓鏈В鍐崇殑闂,鏈涘悇璺ぇ鐗涙寚鏁?
else printf("%s\n",isNP(m) ? "Yes":"No");
}
return 0;
}Input
Input contains numbers a and b (1≤a,b≤100).
Output
Write answer to output.
Sample Input
2 3
Sample Output
-1
涓鐪嬪埌榪欑棰樼洰錛屽氨鎯沖埌楂樼簿搴︾畻娉曪紝Java鐨勫ぇ鏁般?br>姝や婦紜疄嫻佹皳浜嗕竴鐐癸紝涓嶈繃紜疄榪囦簡錛岄剻浜虹殑絎竴棰楽GU鍟婏紝涓嶈鎵撳嚮銆?br>榪囨鏃墮棿鐪嬬湅鑳戒笉鑳藉啓鍑?(C++ Edition) ,鍟ヤ篃鍒浜嗭紝涓婁唬鐮併?br>
imp
imp
public class Solution

{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
BigInteger A=BigInteger.valueOf(a);
BigInteger B=BigInteger.valueOf(b); // A^B
BigInteger rA=BigInteger.valueOf(a);
BigInteger rB=BigInteger.valueOf(b); // store the result after computing
for(int i=1; i<b; i++) // just b-1 times
rA=rA.multiply(A);
for(int i=1; i<a; i++)
rB=rB.multiply(B);
System.out.println(rA.subtract(rB)); // sub
}
}闄勩奀ore Java I銆嬮噷鍏充簬 澶ф暟鐨勭畝鍗曚粙緇嶏紝璁茬殑綆楁瘮杈冩竻鏅扮殑浜嗭細
Big Numbers
If the precision of the basic integer and floating-point types is not sufficient, you can
turn to a couple of handy classes in the java.math package: BigInteger and BigDecimal. These
are classes for manipulating numbers with an arbitrarily long sequence of digits. The
BigInteger class implements arbitrary precision integer arithmetic, and BigDecimal does the
same for floating-point numbers.
Use the static valueOf method to turn an ordinary number into a big number:
BigInteger a = BigInteger.valueOf(100);
Unfortunately, you cannot use the familiar mathematical operators such as + and * to
combine big numbers. Instead, you must use methods such as add and multiply in the big
number classes.
BigInteger c = a.add(b); // c = a + b
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); // d = c * (b + 2)
C++ NOTE: Unlike C++, Java has no programmable operator overloading. There was no way
for the programmer of the BigInteger class to redefine the + and * operators to give the add and
multiply operations of the BigInteger classes. The language designers did overload the + operator
to denote concatenation of strings. They chose not to overload other operators, and they
did not give Java programmers the opportunity to overload operators in their own classes.
Listing 3–6 shows a modification of the lottery odds program of Listing 3–5, updated to
work with big numbers. For example, if you are invited to participate in a lottery in
which you need to pick 60 numbers out of a possible 490 numbers, then this program
will tell you that your odds are 1 in 7163958434619955574151162225400929334117176
12789263493493351 013459481104668848. Good luck!
The program in Listing 3–5 computed the statement
lotteryOdds = lotteryOdds * (n - i + 1) / i;
When big numbers are used, the equivalent statement becomes
lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(BigInteger.valueOf(i));
Listing 3–6 BigIntegerTest.java
1. imp
2. imp
3.
4. /**
5. * This program uses big numbers to compute the odds of winning the grand prize in a lottery.
6. * @version 1.20 2004-02-10
7. * @author Cay Horstmann
8. */
9. public class BigIntegerTest
10. {
11. public static void main(String[] args)
12. {
13. Scanner in = new Scanner(System.in);
14.
15. System.out.print("How many numbers do you need to draw? ");
16. int k = in.nextInt();
17.
18. System.out.print("What is the highest number you can draw? ");
19. int n = in.nextInt();
20.
21. /*
22. * compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k)
23. */
24.
25. BigInteger lotteryOdds = BigInteger.valueOf(1);
26.
27. for (int i = 1; i <= k; i++)
28. lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1)).divide(
29. BigInteger.valueOf(i));
30.
31. System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");
32. }
33. }
java.math.BigInteger
? BigInteger add(BigInteger other)
? BigInteger subtract(BigInteger other)
? BigInteger multiply(BigInteger other)
? BigInteger divide(BigInteger other)
? BigInteger mod(BigInteger other)
returns the sum, difference, product, quotient, and remainder of this big integer and
other.
? int compareTo(BigInteger other)
returns 0 if this big integer equals other, a negative result if this big integer is less
than other, and a positive result otherwise.
? static BigInteger valueOf(long x)
returns a big integer whose value equals x.
? BigDecimal add(BigDecimal other)
? BigDecimal subtract(BigDecimal other)
? BigDecimal multiply(BigDecimal other)
? BigDecimal divide(BigDecimal other, RoundingMode mode) 5.0
returns the sum, difference, product, or quotient of this big decimal and other.
To compute the quotient, you must supply a rounding mode. The mode
RoundingMode.HALF_UP is the rounding mode that you learned in school (i.e., round
down digits 0 . . . 4, round up digits 5 . . . 9). It is appropriate for routine
calculations. See the API documentation for other rounding modes.
? int compareTo(BigDecimal other)
returns 0 if this big decimal equals other, a negative result if this big decimal is less
than other, and a positive result otherwise.
? static BigDecimal valueOf(long x)
? static BigDecimal valueOf(long x, int scale)
returns a big decimal whose value equals x or x /10scale.